In Java RegEx, how to find out the difference between .
(dot) the meta character and the normal dot as we using in any sentence. How to handle this kind of situation for other meta characters too like (*
,+
,/d
,...)
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I am doing some basic array in JGrasp and found that with an accessor method for a char[][] array to use ('.') to place a single dot.
Escape special characters with a backslash.
\.
,\*
,\+
,\\d
, and so on. If you are unsure, you may escape any non-alphabetical character whether it is special or not. See the javadoc for java.util.regex.Pattern for further information.Here is code you can directly copy paste :
And what if mistakenly there are spaces left before or after "." in such cases? It's always best practice to consider those spaces also.
Here, \\s* is there to consider the spaces and give you only required splitted strings.
Solutions proposed by the other members don't work for me.
But I found this :
to escape a dot in java regexp write
[.]
Perl-style regular expressions (which the Java regex engine is more or less based upon) treat the following characters as special characters:
.^$|*+?()[{\
have special meaning outside of character classes,]^-\
have special meaning inside of character classes ([...]
).So you need to escape those (and only those) symbols depending on context (or, in the case of character classes, place them in positions where they can't be misinterpreted).
Needlessly escaping other characters may work, but some regex engines will treat this as syntax errors, for example
\_
will cause an error in .NET.Some others will lead to false results, for example
\<
is interpreted as a literal<
in Perl, but inegrep
it means "word boundary".So write
-?\d+\.\d+\$
to match1.50$
,-2.00$
etc. and[(){}[\]]
for a character class that matches all kinds of brackets/braces/parentheses.If you need to transform a user input string into a regex-safe form, use
java.util.regex.Pattern.quote
.Further reading: Jan Goyvaert's blog RegexGuru on escaping metacharacters
I wanted to match a string that ends with ".*" For this I had to use the following:
Kinda silly if you think about it :D Heres what it means. At the start of the string there can be any character zero or more times followed by a dot "." followed by a star (*) at the end of the string.
I hope this comes in handy for someone. Thanks for the backslash thing to Fabian.