-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
  • 525
  • 7
  • 20

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
  • 172,303
  • 28
  • 230
  • 274