rrg
rrg

Reputation: 1

regular expression: quotes within quotes

I need to do following type of regular expression match

E.g if i have a string - some "This is "sample" data" example

I want to extract "This is "sample" data" out of the above string. Could please give me a regular expression which can return me desired results

More Details

I have a string as - keyword = "This is "blood" sample" AND name = "some text". I need to extract keyword = "This is "blood" sample" name = "some text" out of it as two separate strings.

Could you please suggest a regular expression for this kind of thing

I used (keywords|name|title) = (.*?(\\\".*\\\").*?) as regular expression and it does not work as expected. It returns me the whole string as it.

Thanks in advance.

Upvotes: 0

Views: 7325

Answers (1)

aioobe
aioobe

Reputation: 420951

Not quite sure what you're after, so I'll fill in the gaps by guessing.

Pattern p = Pattern.compile(".*?(\\\".*\\\").*?");
Matcher m = p.matcher("some \"This is \"sample\" data\" example");

if (m.matches())
    System.out.println(m.group(1));

Output:

"This is "sample" data"

Some remarks:

  • What should be matched in a b "c d "e f "g h" i j" k l" m n?
  • What should be matched in a "b" c"?
  • If you want to treat " and " as opening and closing parenthesis, regular expressions are not for you.

Upvotes: 2

Related Questions