Abdennour TOUMI
Abdennour TOUMI

Reputation: 93421

How to use regex contains " with Pattern.compile

i have this regex :

url\(\s*(['"]?+)(.*?)\1\s*\)

Which retrieves all url from css file .

I use this code , However i get an error :

Pattern p = Pattern.compile("url\(\s*(['\"]?+)(.*?)\1\s*\)");
      Matcher m = p.matcher(cssText);
      while (m.find()) {
          println m.group()
        }

As you can note that i add \ to esacpe " . But in vain

Upvotes: 1

Views: 92

Answers (1)

anubhava
anubhava

Reputation: 785761

You need to use \\ instead of \ in Java regex:

Pattern p = Pattern.compile( "url\\(\\s*(['\"]?+)(.*?)\\1\\s*\\)" );
  • 1st \ is for escaping from String object
  • 2nd \ is for escaping from underlying regex engine

Upvotes: 3

Related Questions