简体   繁体   中英

How do I escape a special Regex character?

I've encountered a problem in Java, specific to the String.split function. I'm porting my C# code to Java, and something bothers Java. My C# code is as follows:

foreach (char sep in separators)
    if (text.Contains(sep.ToString()))
        array = text.Reverse().Split(sep);

Reverse() is an extension I've made myself which just reverses a string. separators is a char array that contains a few separators:

char[] separators = { '&', '!', '#', '?', '%' };

Now, in Java, my code is as follows:

for (char sep : separators) {
    String sepp = String.valueOf(sep);
    if (text.contains(sepp))
        array = new StringBuilder(text).reverse().toString().split(sepp);
}

The problem with this code is that, when I have the specific separator ? (it's randomly choosed), then it throws me this error (Java only):

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
at java.base/java.util.regex.Pattern.error(Pattern.java:2027)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2202)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2068)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1782)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1428)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1068)
at java.base/java.lang.String.split(String.java:2317)
at java.base/java.lang.String.split(String.java:2364)
at com.anerruption.encryption.Main.decrypt(Main.java:58)
at com.anerruption.encryption.Main.main(Main.java:43)

My guess is that the function string.Split in C# doesn't use Regex, but the one in Java does. From what I've heard, I need to escape the character. Doing \\? did not work. How could I do this?

Any help is greatly appreciated!

you can use this way, by concatenating "\\" to each separator only for split() method

char[] separators = { '&', '!', '#', '?', '%' };

for (char sep : separators) {
    if (text.contains(sep+""))
       array = new StringBuilder(text).reverse().toString().split("\\"+sep);
}

Meta Character Description.........................................Escape character

*   zero or more occurrence of the characters        \\*
+   one or more occurrence of the characters         \\+ 
?   zero or only one occurrence of the characters    \\? 
^   Start of the character sequence                  \\^
$   End of the character sequence                    \\$

Example of?

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] separators = { "&", "!", "#", "\\?", "%" };
    String str = "Parth?Prem";
    String str2 = "\\?";
    String[] strArray;

    strArray = str.split(str2);

    System.out.println("Given String : " + str);
    for(int i=0;i<strArray.length;i++) {
        System.out.println("strArray["+i+"] = " + strArray[i]);
    }
    
}

Output:

Given String: Parth?Prem

strArray[0] = Parth

strArray[1] = Prem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM