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("\\$");
Related
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("\\|-\\|")
I have html code with img src tags pointing to urls. Some have mysite.com/myimage.png as src others have mysite.com/1234/12/12/myimage.png. I want to replace these urls with a cache file path. Im looking for something like this.
String website = "mysite.com"
String text = webContent.replaceAll(website+ "\\d{4}\\/\\d{2}\\/\\d{2}", String.valueOf(cacheDir));
This code however does not work when the url does not have the extra date stamp at the end. Does anyone know how i might achieve this? Thanks!
Try this one
mysite\.com/(\d{4}/\d{2}/\d{2}/)?
here ? means zero or more occurance
Note: use escape character \. for dot match because .(dot) is already used in regex
Sample code :
String[] webContents = new String[] { "mysite.com/myimage.png",
"mysite.com/1234/12/12/myimage.png" };
for (String webContent : webContents) {
String text = webContent.replaceAll("mysite\\.com/(\\d{4}/\\d{2}/\\d{2}/)?",
String.valueOf("mysite.com/abc/"));
System.out.println(text);
}
output:
mysite.com/abc/myimage.png
mysite.com/abc/myimage.png
You are missing a forward slash between the website.com and the first 4 digits.
String text = webContent.replaceAll(Pattern.quote(website) + "/\\d{4}\\/\\d{2}\\/\\d{2}", String.valueOf(cacheDir));
I'd also recommend using a literal for your website.com value (the Pattern.quote part).
Finally you are also missing the last forward slash after the last two digits so it won't be replaced, but that may be on purpose...
Try:
String text = webContent.replaceAll("(?<="+website+")(.*)(?=\\/)",
String.valueOf(cacheDir));
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.
I am trying to replace '\\'with '/' in java(Android) and this does not seem to work!
String rawPath = filePath.replace("\\\\", "/");
What is wrong with this ? I have escaped "\" and tried escaping '/' but to no use. Nothing happens to the original string.
filePath = abc\\xyz(not after escaping two \\, the original string is with two \\)
rawPath = abc \ xyz
expected = abc/xyz
Whats the correct way of doing this? (Another Windows file to Android path conversion prob)
When using String.replace(String, String) the backslash doesn't need to be escaped twice (thats when using replaceAll - it deals with regex). So:
String rawPath = filePath.replace("\\", "/");
Or using char version:
String rawPath = filePath.replace('\\', '/');
You do not need the quad-druple escape,
\\\\
, just simply
\\
.
escape with single slash should be enough. Following is working fine for me.
String rawPath = filePath.replace("\\", "/");
public static void main(String[] args) {
String s = "foo\\\\bar";
System.out.println(s);
System.out.println(s.replace("\\\\", "/"));
}
will print
foo\\bar
foo/bar
If you want to replace a sequence of 2 backslashes in your original string with a single forward slash, this should work:
String filePath = "abc\\\\xyz";
String rawPath = filePath.replace("\\\\", "/");
System.out.println(filePath);
System.out.println(rawPath);
outputs:
abc\\xyz
abc/xyz
Do you really have two backslashes in the String in the first place? That only appears in Java source code. At runtime there will only be one backslash. So the task reduces to changing backslashes to forward slashes (why?). For which you need a regex if you are using replaceAll(), which would require four of them: two for the compiler, and two for the regex, but you aren't using that, you are using replace(), which isn't a regex, so you only need two, one for the compiler and one for itself.
Why are you doing this? It is never necessary to use a backslash in a File path in Java at all, and it is also never necessary to translate them to / unless you are doing URL-like things with them, in which case there are File.toURI() methods and URI and URL classes for that.
Here is a very small method to get the desktop path and show you how to replace them in the return statement.
public static String getDesktopPath() {
String desktopPath = System.getProperty("user.home") + "/Desktop";
return desktopPath.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*", ",");