i am not familiar with regular expressions maybe one of you can help me. I have a String "46,50 EUR" or "-4.785,20 €" or something similar. I like to remove all chars that are not "0123456789.,-" I tried:
string betrag = "-4.785,20 €";
betrag = betrag.replaceAll("/[^0-9.,-]/", "");
but it wont work. The EUR-Sign will not be removed. Maybe it has something to do with the coding? utf-8 vs. latin1? Or my regular expression is wrong?
Java's regex literals do not require delimiters, so remove the /:
String betrag = "-4.785,20 €";
betrag = betrag.replaceAll("[^0-9.,-]+", "");
System.out.println(betrag); // -4.785,20
you can also use "/d" - it is shortcut for digital characters
but of course the result is the same as mentioned by Tim
String betrag = "-4.785,20 €";
betrag = betrag.replaceAll("[^\d.,-]+", "");
by the way you can easily test your regular expression via some tools/websites - e.g.:
https://www.freeformatter.com/java-regex-tester.html
Related
I want to replace "\" with this "/" in my string.
I am using method replaceAll for this. But it is giving me error.
String filePath = "D:\pbx_u01\apache-tomcat-6.0.32\bin\uploadFiles\win.jpg";
String my_new_str = filePath.replaceAll("\\", "//");
Just use replace.
The method replaceAll takes a regular expression and yours would be malformed.
String filePath = "D:/pbx_u01/apache-tomcat-6.0.32/bin/uploadFiles/win.jpg";
System.out.println(filePath.replace("/", "\\"));
Output
D:\pbx_u01\apache-tomcat-6.0.32\bin\uploadFiles\win.jpg
When you absolutely want to use regex for this, use:
String filePath = "D:\\pbx_u01\\apache-tomcat-6.0.32\\bin\\uploadFiles\\win.jpg";
String my_new_str = filePath.replaceAll("\\\\", "/");
Output of my_new_str would be:
D:/pbx_u01/apache-tomcat-6.0.32/bin/uploadFiles/win.jpg
Just be sure to notice the double backslashes \\ in the source String (you used single ones \ in your question.)
But Mena showed in his answer a much simpler, more readable way to achive the same. (Just adopt the slashes and backslashes)
You are unable because character '//' should be typed only single '/'.
String filePath = "D:\\pbx_u01\\apache-tomcat-6.0.32\\bin\\uploadFiles\\win.jpg"
String my_new_str = filePath.replaceAll("\\", "/");
Above may be fail during execution giving you a PatternSyntaxException, because the first String is a regular expression so you use this,
String filePath = "D:\\pbx_u01\\apache-tomcat-6.0.32\\bin\\uploadFiles\\win.jpg"
String my_new_str = filePath.replaceAll("\\\\", "/");
Check this Demo ideOne
I am using the code in Java:
String word = "hithere";
String str = "123hithere12345hi";
output(str.replaceAll("(?!"+word+")", "x"));
However, rather than outputting: xxxhitherexxxxxxx like I want it to, it outputs: x1x2x3hxixtxhxexrxex1x2x3x4x5xhxix x, I've tried a load of different regex patterns to try to do this, but I can't seem to figure out how to do this :(
Any help would be much appreciated.
Well this technically works. Using only replace all and only one line, and it's assuming you string does not contain a deprecated ASCII character (BEL)
String string = "hithere";
String string2 = "asdfasdfasdfasdfhithereasasdf";
System.out.println(string2.replaceAll(string,"" + (char)string.length()).replaceAll("[^" + (char)string.length() + "]", "x").replaceAll("" + (char)string.length(), string));
I think this is what you're looking for, if I'm not mistaken:
String pattern = "(\\d)|(hi$)";
System.out.println("123hithere12345hi".replaceAll(pattern, "X"));
The pattern replaces any numeric digits and the word "hi".
This lookaround based code will work for you:
String word = "hithere";
String string = "123hithere12345hi";
System.out.println(string.replaceAll(
".(?=.*?\\Q" + word + "\\E)|(?<=\\Q" + word + "\\E(.){0,99}).", "x"));
//=> xxxhitherexxxxxxx
I have a regex like below one :
"\\t'AUR +(username) .*? /ROLE=\"(my_role)\".*$"
username and my_role parts will be given from args. So they always change when the script is starting. So how can i give parameters to that part of regex ?
Thanks for your helps.
Define regex like this:
String fmt = "\\t'AUR +(%s) .*? /ROLE=\"(%s)\".*$";
// assuming userName and myRole are your arguments
String regex = String.format(fmt, userName, myRole);
You should escape special characters in dynamic strings using Pattern.quote. To put the regex parts together you can simply use string concatenation like this:
String quotedUsername = Pattern.quote(username);
String quotedRole = Pattern.quote(my_role);
String regexString = "\\t'AUR +(" + quotedUsername +
") .*? /ROLE=\"(" + quotedRole + ")\".*$";
I think mixing regular expressions with format strings when using String.format can make the regex harder to understand.
Use string format or straight string concat to construct the regex before passing it to compile ...
Try this for an example:
String patternString = "\\t'AUR +(%s) .*? /ROLE=\"(%s)\".*$";
String formatted = String.format(patternString, username,my_role);
System.out.println(formatted);
Pattern pattern = Pattern.compile(patternString);
You can run a working example here: http://ideone.com/93YeNg
Is there a more efficient way of splitting a string than this?
String input = "=example>";
String[] split = input.split("=");
String[] split1 = split[1].split(">");
String result = split1[0];
The result would be "example".
String result = input.replaceAll("[=>]", "");
Very simple regex!
To learn more, go to this link: here
Do you really need regex. You can do:
String result = input.substring(1, input.length()-1);
Otherwise if you really have a case for regex then use character class:
String result = input.replaceAll("[=>]", "");
If you just want to get example out of that do this:
input.substring(1, input.lastIndexOf(">"))
If the string of yours defenitely constant format use substring otherwise go fo regex
result = result.substring(1, result.length() - 1);
You can do it more elegant with RegEx groups:
String sourceString = "=example>";
// When matching, we can "mark" a part of the matched pattern with parentheses...
String patternString = "=(.*?)>";
Pattern p = Pattern.compile(patternString);
Matcher m = p.matcher(sourceString);
m.find();
// ... and access it later
String result = m.group(1);
You can try this regex: ".*?((?:[a-z][a-z]+))"
But it would be better when you use something like this:
String result = input.substring(1, input.length()-1);
try this
String result = input.replace("[\\W]", "")
You can try this too
String input = "=example>";
System.out.println(input.replaceAll("[^\\p{L}\\p{Nd}]", ""));
This will remove all non-words characters
Regex would do the job perfectly, but just to add something new for future solutions you also could use a third party lib such as Guava from Google, it adds a lot of functionalities to your project and the Splitter is really helpful to solve something like you have.
This is just an example code of the thing i try to accomplish.
String s = "hello(1234aA)something";
String replaceString = "(1234aa)";
String s2 = s.replaceAll("(i?)" + replaceString, "something");
The String s is going to be the same but can differ in case, thats why i use (i?) in replaceall.
How can i make regex ignore the special
Use quote(), it seems you've already figured out the ignore case, but you should use (?i), not (i?).
String s = "hello(1234aA)something";
String replaceString = "(?i)" + Pattern.quote("(1234aa)");
String s2 = s.replaceAll(replaceString, "something");
This should work.