-1

I want to fetch the word outside a quotation for example if I have this text:

  xxxx xxx OK xxxxx "xxx OK xxxx" 

I want to fetch the "OK" outside the quotation which mean first one I make this regular expression

  [ ]OK[ ]

but it fetch the both. So how can I fetch only outside a quotation with regular expression?

Aymn Alaney
  • 495
  • 7
  • 16

1 Answers1

1

You may use the combination of caturing group and the regex alternation operator.

"[^"]*"|( OK )

And now the group index 1 contain the <space>OR<space> which exists outside the double quoted part.

Code:

Matcher m = Pattern.compile("\"[^\"]*\"|( OK )").matcher(s);
while(m.find())
{
System.out.println(m.group(1));
}
Avinash Raj
  • 166,785
  • 24
  • 204
  • 249