This question already has answers here:
How to escape text for regular expression in Java?
(8 answers)
Closed 9 years ago.
Is there a library or any easy way to convert a string and make sure its compatible as a regex to look for and replace in another string. So if the string is "$money" it would get converted to "\$money". I tried using StringEscapeUtil.escape but it doesn't work with characters such as $.
You can use Pattern.quote("$money").
Prepend the \\Q in front of the string, and \\E at the end:
"\\Q$money\\E"
This tells the regex engine that the string between \Q and \E must be interpreted verbatim, ignoring any metacharacters that it may contain.
Related
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 2 years ago.
I am new to java. I am practicing String methods currently. I wanted to check if the email contains "gmail.com" or not.
This is the code I came up with
System.out.println(domain.matches(".*gmail.com(.*)"));
but dot(.) here means any character so even if i pass the string as "xyz#gmailpcom" it will return true. Basically I want to check dot(.) in the string without considering it as regular expression.
If you just want to check whether it contains gmail.com, then just use contains:
System.out.println(domain.contains("gmail.com"));
If at a later stage you want to use a regular expression but escape a dot, do that with a backslash in the regular expression, which needs to be escaped again for use in a Java string literal:
domain.matches(".*gmail\\.com(.*)")
you can also check it bu checking the indexOf or lastIndexOf:
System.out.println(domain.indexOf("gmail.com") != -1);
System.out.println(domain.lastIndexOf("gmail.com") != -1);
This question already has answers here:
Regular Expressions: How to Express \w Without Underscore
(7 answers)
Closed 2 years ago.
Currently have this regex string in my java code:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It successfully accepts those characters, however it also accepts underscore, how can modify the regex to reject underscore appearing anywhere in the string?
Your regex is:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It is using \w which is equivalent of [a-zA-Z0-9_], hence it allows underscore also.
You can change your character class to this:
^[-#. a-zA-Z0-9\\/]{0,70}$
Note that space, dot, #, / don't need to be escaped inside [...] and - if placed at first or last position doesn't require escaping either.
This question already has answers here:
Regex for removing trailing zeros
(5 answers)
Closed 7 years ago.
Although I have seen a question similar to this one asked quite a few times, I actually mean remove all trailing zeroes.
I would like to convert something like
"1903895810000"
to
"190389581"
I am looking for a String.replace() solution
Simple regexp with replaceAll will do it.
String s = "1903895810000";
System.out.println(s.replaceAll("0+$", ""));
[EDIT]:
s.replace(0, "") will not work here, because it will remove all zeros from the string, so you can't use it. So, here I used replaceAll, that uses regular expressions to match replacement string. This simple regexp 0+$ matches any number of zeros 0+ followed by end-of-string $, so it would be "some zeroes at the end".
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 9 years ago.
I'm trying to split a string at every '.' (period), but periods are a symbol used by java regexes. Example code,
String outstr = "Apis dubli hre. Agro duli demmos,".split(".");
I can't escape the period character, so how else do I get Java to ignore it?
Use "\\." instead. Just using . means 'any character'.
I can't escape the period character, so how else do I get Java to ignore it?
You can escape the period character, but you must first consider how the string is interpreted.
In a Java string (that is fed to Pattern.compile(s))...
"." is a regex meaning any character.
"\." is an illegally-escaped string. This won't compile. As a regex in a text editor, however, this is perfectly legitimate, and means a literal dot.
"\\." is a Java string that, once interpreted, becomes the regular expression \., which is again the escaped (literal) dot.
What you want is
String outstr = "Apis dubli hre. Agro duli demmos,".split("\\.");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to escape text for regular expression in Java
Is there a built in way or a standard library for cleaning arbitrary strings for use in regex?
As in, if I have the string something .* foo and I want to turn that into a regex like ^something \.\* foo$ is that something that can be easily done?
You can use Pattern.quote(String) for this purpose. From the docs:
Returns a literal pattern String for the specified String.
This method produces a String that can be used to create a Pattern that would
match the string s as if it were a literal pattern.
Metacharacters or escape sequences in the input sequence will be given
no special meaning.