Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match both slash in JAVA

Tags:

I want to match forward slash / or back slash \ in particular string for e.g.:
1. Hi/Hello/Bye/
2. Hi\Hello\Bye\
3. Hi\Hello/Bye\
4. HiHelloBye
In the given strings only the last record should not be matched because it does not contain either / or \.

What I am using

if (strFile.matches(".*//.*")) {     //String Matches. } else {     //Does not match. } 

This matches for forward slash / only. I don't know how to write regex for both slash (for OR condition).

like image 532
Himanshu Jansari Avatar asked Aug 03 '12 04:08

Himanshu Jansari


People also ask

How do you match a slash in a regular expression in Java?

If you want to match a backslash in your regular expression, you'll have to escape it. Backslash is an escape character in regular expressions. You can use '\\' to refer to a single backslash in a regular expression. However, backslash is also an escape character in Java literal strings.

How do you put a slash in RegEx?

The forward slash character is used to denote the boundaries of the regular expression: ? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters.

What does \\ mean in Java RegEx?

The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.


1 Answers

The "character" you're looking to match would be:

"[/\\\\]" 

duplicating the backslash first for the string then again for the regex.

This is perhaps the nastiest bit of regexes when you need to use backslashes in languages that also use the backslash for escaping strings.

The Java compiler sees the string "\\\\" in the source code and actually turns that into "\\" (since it uses \ as an escape character).

Then the regular expression sees that "\\" and, because it also uses \ as an escape character, will treat it as a single \ character.

As Liu Yan points out in a comment, you could get rid of one level of backslashes (the regex one) by using one of the following:

".*[/\\x5c].*" ".*[/\\u005c].*" 

That might make it slightly more readable.

Once all that reduction is done, you have specified a character class consisting of both slashes and, if the character in question matches either of them, it returns true.

The following code shows this in action:

public class testprog {     public static void checkString (String s) {         boolean yes = s.matches(".*[/\\\\].*");         System.out.println ("'" + s + "': " + yes);     }      public static void main (String s[]) {         checkString ("Hi/Hello/Bye/");         checkString ("Hi\\Hello\\Bye\\");         checkString ("Hi\\Hello/Bye\\");         checkString ("HiHelloBye");     } } 

and it outputs:

     'Hi/Hello/Bye/': true     'Hi\Hello\Bye\': true     'Hi\Hello/Bye\': true     'HiHelloBye': false 
like image 127
paxdiablo Avatar answered Sep 28 '22 11:09

paxdiablo