Java regex pattern to split String based on delimiter string "|-|" - java

I have a java string delimited by |-| like below.
Can't find |-| deliter based split any where else this is unique.
String agent = "iOS|-|iPhone|-|18.2.3|-|kuoipo-kjpopoo-kijhloii-kllkijii";
What is the correct regex to split the contents in string Array like below.
String[] dataarray;
dataarray[0]="iOS";
dataarray[1]="iPhone";
dataarray[2]="18.2.3";
dataarray[3]="kuoipo-kjpopoo-kijhloii-kllkijii";
Already tried:
agent.split("\\|-\\|");
Thanks in Advance.

Won't work
agent.split("|-|")
Do
agent.split("\\|-\\|")

Related

Can't split string in Java

I have trouble with the split function in Java. When I try to split a string with a regex "$"
String line = "Vu Quang Huy$2/11/1999$Ha Noi$Nam$CNTT$1.2$12$10000.0";
String[] properties = line.split("$");
It doesn't do any thing. The properties at index 0 is the same as the original string
System.out.println(properties[0]);
And it shows
Vu Quang Huy$2/11/1999$Ha Noi$Nam$CNTT$1.2$12$10000.0
Can anyone help me with this problem? Thanks in advance!
$ in regex means "the end of a string", use \$ instead.
And, you have to escape the '\' as well, so you have to write it like this
String[] properties = line.split("\\$");

How to replace a given substring with "" from a given string?

I went through a couple of examples to replace a given sub-string from a given string with "" but could not achieve the result. The String is too long to post and it contains a sub-string which is as follows:-
/image/journal/article?img_id=24810&t=1475128689597
I want to replace this sub-string with "".Here the value of img_id and t can vary, so I would have to use regular expression. I tried with the following code:-
String regex="^/image/journal/article?img_id=([0-9])*&t=([0-9])*$";
content=content.replace(regex,"");
Here content is the original given string. But this code is actually not replacing anything from the content. So please help..any help would be appreciated .thanx in advance.
Use replaceAll works in nice way with regex
content=content.replaceAll("[0-9]*","");
Code
String content="/image/journal/article?img_id=24810&t=1475128689597";
content=content.replaceAll("[0-9]*","");
System.out.println(content);
Output :
/image/journal/article?img_id=&t=
Update : simple, might be little less cozy but easy one
String content="sas/image/journal/article?img_id=24810&t=1475128689597";
content=content.replaceAll("\\/image.*","");
System.out.println(content);
Output:
sas
If there is something more after t=1475128689597/?tag=343sdds and you want to retain ?tag=343sdds then use below
String content="sas/image/journal/article?img_id=24810&t=1475128689597/?tag=343sdds";
content=content.replaceAll("(\\/image.*[0-9]+[\\/])","");
System.out.println(content);
}
Output:
sas?tag=343sdds
If you're trying to replace the substring of the URL with two quotations like so:
/image/journal/article?img_id=""&t=""
Then you need to add escaped quotes \"\" inside your content assignment, edit your regex to only look for the numbers, and change it to replaceAll:
content=content.replaceAll(regex,"\"\"");
You can use Java regex Utility to replace your String with "" or (any desired String literal), based on given pattern (regex) as following:
String content = "ALPHA_/image/journal/article?img_id=24810&t=1475128689597_BRAVO";
String regex = "\\/image\\/journal\\/article\\?img_id=\\d+&t=\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String replacement = matcher.replaceAll("PK");
System.out.println(replacement); // Will print ALPHA_PK_BRAVO
}

Extract String from another string in Java

Here is a string:
"http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"
This string is a concatenation of two URLs. I would like to extract only the second URL:
"http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"
How can I do that using Java?
Remove everything up to "http://" not found at the start:
String url2 = str.replaceAll("(?i).+(?=https?://)", "");
This will work case insensitively and match http or https protocols.
Try this. "str" is the url string
System.out.println(str.substring(str.lastIndexOf("http:")));
If you want to extract the URL, just find the last instance of http, and take the substring:
String secondUrl = firstUrl.substring(firstUrl.lastIndexOf("http"));
Try using string's .split() method, like this:
String oneURL = twoURLs.split("(?<!^)(?=http://)")[1];
This splits the string in places that are not at the end of the string, but are followed by http://. With that, you should end up with an array like this:
["http://l2.yimg.com/bt/api/res/1.2/iis49xBsStLiYI6LjauR6Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/", "http://media.zenfs.com/fr_FR/News/LeMonde.fr/1515504_3_f73c_le-cyber-harcelement-est-une-realite-trop-lo_450282425a88c544c2ff4121a5d9dab4.jpg"]
[1] takes only the second element of that array.
Explanation and demonstration of the regex here: http://regex101.com/r/eW6mZ0

Replacing special character from a String in Android

I have a String as folder/File Name. I am creating folder , file with that string. This string may or may not contain some charters which may not allow to create desired folder or file
e.g
String folder = "ArslanFolder 20/01/2013";
So I want to remove these characters with "_"
Here are characters
private static final String ReservedChars = "|\?*<\":>+[]/'";
What will be the regular expression for that? I know replaceAll(); but I want to create a regular expression for that.
Use this code:
String folder = "ArslanFolder 20/01/2013 ? / '";
String result = folder.replaceAll("[|?*<\":>+\\[\\]/']", "_");
And the result would be:
ArslanFolder 20_01_2013 _ _ _
you didn't say that space should be replaced, so spaces are there... you could add it if it is necessary to be done.
I used one of this:
String alphaOnly = input.replaceAll("[^\\p{Alpha}]+","");
String alphaAndDigits = input.replaceAll("[^\\p{Alpha}\\p{Digit}]+","");
See this link:
Replace special characters
Try this :
replaceAll("[\\W]", "_");
It will replace all non alphanumeric characters with underscore
This is correct solution:
String result = inputString.replaceAll("[\\\\|?\u0000*<\":>+\\[\\]/']", "_");
Kent answer is good, but he isnt include characters NUL and \.
Also, this is a secure solution for replacing/renaming text of user-input file names, for example.

Java regex replace

I've a csv string like
"abc, java, stackoverflow , stack exchange , test"
Can I use regex to remove the space around the commas to get a string like
"abc,java,stackoverflow,stack exchange,test"
str = str.replaceAll("\\s*,\\s*", ",");

Categories

Resources