0

I have following regex pattern working fine in Javascript :

Pattern-

/\<span\>(\d{2}-\d{2}-\d{4})\s*?(\d{1,2}:\d{2}\s*?(?:am|pm))\s*?(?:<\/SPAN><BR\/?><SPAN>)?\s*?((\d[ -]*?){13,17})\s*?\<\/span\>/i 

String -

<SPAN>06-24-2015  11:28AM  0250 01 90775 05342</SPAN>

But I am getting an error when passing same pattern string to Pattern.compile() function .

Error is : Invalid escape sequence .

Here is what I tried :

Pattern p = Pattern.compile("/\<span\>(\d{2}-\d{2}-\d{4})\s*?(\d{1,2}:\d{2}\s*?(?:am|pm))\s*?(?:<\/SPAN><BR\/?><SPAN>)?\s*?((\d[ -]*?){13,17})\s*?\<\/span\>/i ");  

Matcher m = p.matcher("<SPAN>06-24-2015  11:28AM  0250 01 90775 05342</SPAN>");
boolean b = m.matches();

Please suggest how can i correct this .

Thanks

Edit

I changed my code to following after knowing comments, but now I cannot match the pattern:

Pattern p = Pattern.compile(
        "(?i)/\\<span\\>(\\d{2}-\\d{2}-\\d{4})\\s*?(\\d{1,2}:\\d{2}\\s*?(?:am|pm))\\s*?(?:<\\/SPAN><BR\\/?><SPAN>)?\\s*?((\\d[ -]*?){13,17})\\s*?\\<\\/span\\>");

Matcher m = p.matcher("<SPAN>06-24-2015  11:28AM  0250 01 90775 05342</SPAN>");
boolean b = m.matches();

System.out.println("Do regex pattern matches : " + b);
4
  • 3
    Double the backslashes, and replace the / at the start with (?i) and remove /i at the end. The rest is up to you to check. And avoid escaping / - it is not a special regex metacharacter. Commented Apr 10, 2017 at 10:46
  • use \\ instead of \. Like \\d instead of \d. Commented Apr 10, 2017 at 10:47
  • 1
    but now I cannot match the pattern - Because there is / after (?i). Do not escape /, < and >. Commented Apr 10, 2017 at 10:57
  • thanks @WiktorStribiżew can you please tell from where i can learn more on this ? Commented Apr 10, 2017 at 11:04

3 Answers 3

1

This one works. The only change derived from your edit was to remove the slash at the beginning. This was a simple JavaScript artifact.

Pattern p = Pattern.compile(
                "(?i)\\<span\\>(\\d{2}-\\d{2}-\\d{4})\\s*?(\\d{1,2}:\\d{2}\\s*?(?:am|pm))\\s*?(?:<\\/SPAN><BR\\/?><SPAN>)?\\s*?((\\d[ -]*?){13,17})\\s*?\\<\\/span\\>");
Matcher m = p.matcher("<SPAN>06-24-2015  11:28AM  0250 01 90775 05342</SPAN>");
boolean b = m.matches();
        System.out.println("Do regex pattern matches : " + b);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Pattern.CASE_INSENSITIVE field option to make your regex case insensitive instead of using (?i) flag in your regex:

Pattern p = Pattern.compile(
    "\\<span\\>(\\d{2}-\\d{2}-\\d{4})\\s*?(\\d{1,2}:\\d{2}\\s*?(?:am|pm))\\s*?(?:<\\/SPAN><BR\\/?><SPAN>)?\\s*?((\\d[ -]*?){13,17})\\s*?\\<\\/span\\>",
Pattern.CASE_INSENSITIVE);

Matcher m = p.matcher("<SPAN>06-24-2015  11:28AM  0250 01 90775 05342</SPAN>");

System.out.println("Do regex pattern matches : " + m.matches());

Note:

And also as mentioned in comments in Java you don't need to wrap your regex between slaches /, you need to update your regex string and remove the leading / from it and it will work as expected.

This is a working Demo.

2 Comments

its not able to match either ways .
@Softxide You will need to update your regex string and remove the leading /, see my Edit.
1

You should remove the first / character and use Pattern.CASE_INSENSITIVE. For example:

String regex = "\\<span\\>(\\d{2}-\\d{2}-\\d{4})\\s*?(\\d{1,2}:\\d{2}\\s*?(?:am|pm))\\s*?(?:<\\/SPAN><BR\\/?><SPAN>)?\\s*?((\\d[ -]*?){13,17})\\s*?\\<\\/span\\>";

Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

Matcher m = p.matcher("<SPAN>06-24-2015  11:28AM  0250 01 90775 05342</SPAN>");
boolean b = m.matches();
System.out.println("result: "  + b);

Output:

result: true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.