hell_storm2004 hell_storm2004 - 1 year ago 518
Java Question

Illegal/unsupported escape sequence near index

I was trying use a regex to find some matches in a string in Java. The actual regex is

^(interface \X*!)

When i do it Java i use

^(interface \\X*!)

Now this throws Illegal/unsupported escape sequence near index 13. I searched the boards a little bit and found that it should actually be four backslashes to make it work. But if i use

^(interface \\\\X*!)

it returns no matches. Any pointers would be really helpful.

Just a sample match would be like

interface ABC

Answer Source

In order to match the pattern you identify in the comments, using Java, something like this should work:

    Pattern p = Pattern.compile("interface[^!]*!", Pattern.DOTALL);
    Matcher m = p.matcher("interface ABC\ntemp\nabc\nxyz\n!"); // your test string
    if (m.matches()) {

This pattern matches any string beginning with "interface", followed by zero or more of any character except "!", followed by "!".

Pattern.DOTALL tells it to match carriage returns and line feeds. See this for more info on DOTALL.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download