Java RegEx meta character (.) and ordinary dot?

JavaRegex

Java Problem Overview


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,...)

Java Solutions


Solution 1 - Java

If you want the dot or other characters with a special meaning in regexes to be a normal character, you have to escape it with a backslash. Since regexes in Java are normal Java strings, you need to escape the backslash itself, so you need two backslashes e.g. \\.

Solution 2 - Java

Solutions proposed by the other members don't work for me.

But I found this :

to escape a dot in java regexp write [.]

Solution 3 - Java

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 in egrep it means "word boundary".

So write -?\d+\.\d+\$ to match 1.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

Solution 4 - Java

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.

Solution 5 - Java

Here is code you can directly copy paste :

String imageName = "picture1.jpg";
String [] imageNameArray = imageName.split("\\.");
for(int i =0; i< imageNameArray.length ; i++)
{
   system.out.println(imageNameArray[i]);
}

And what if mistakenly there are spaces left before or after "." in such cases? It's always best practice to consider those spaces also.

String imageName = "picture1  . jpg";
String [] imageNameArray = imageName.split("\\s*.\\s*");
    for(int i =0; i< imageNameArray.length ; i++)
    {
       system.out.println(imageNameArray[i]);
    }

Here, \\s* is there to consider the spaces and give you only required splitted strings.

Solution 6 - Java

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.

Solution 7 - Java

If you want to end check whether your sentence ends with "." then you have to add [\.\]$ to the end of your pattern.

Solution 8 - Java

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.

Solution 9 - Java

I was trying to split using .folder. For this use case, the solution to use \\.folder and [.]folder didn't work.

The following code worked for me

String[] pathSplited = Pattern.compile("([.])(folder)").split(completeFilePath);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJavaUserView Question on Stackoverflow
Solution 1 - JavaFabian SteegView Answer on Stackoverflow
Solution 2 - JavaKaelView Answer on Stackoverflow
Solution 3 - JavaTim PietzckerView Answer on Stackoverflow
Solution 4 - JavaChristoffer HammarströmView Answer on Stackoverflow
Solution 5 - JavaDevaView Answer on Stackoverflow
Solution 6 - JavaAtspulgsView Answer on Stackoverflow
Solution 7 - JavaMadhu GogineniView Answer on Stackoverflow
Solution 8 - JavargmView Answer on Stackoverflow
Solution 9 - JavaDeepak PatankarView Answer on Stackoverflow