0

I am trying to get groups which match the pattern. Input String is class equals

'one and two!' and 'three or four five' and 'six'

I have tried following pattern. But it matches and which is not within single quotes

(?:'(?:\S*\s*)*(and|or)+(?:\s*\S*)*')+

I want groups like

'one and two!'
'three or four five'

All String which has and|or within single quotes should be matched. within single quote it can have special characters and many spaces etc

How can i alter the pattern which i have above?

HamZa
  • 14,671
  • 11
  • 54
  • 75
Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55
  • Can you also have embedded single quotes? – fge Apr 08 '14 at 08:09
  • Embedded single quotes? You mean something like this 'one and 'two''? Yes it can have any special character. like 'one and two's' is possible – Dheeraj Joshi Apr 08 '14 at 08:10
  • And they won't even be escaped? Then you can't do that with regexes – fge Apr 08 '14 at 08:13
  • Nope. Atleast can i do it if there are no quotes inside quotes? I can ask API provider to wrap everything in single quote and text wont have single quote – Dheeraj Joshi Apr 08 '14 at 08:25

2 Answers2

1

try this

"'.+?(\\s(and|or)\\s).+?'"
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

Provided there are no single quotes in your single quotes then you can use a pattern such as:

final Pattern PATTERN = Pattern.compile("('[^']+')( (and|or) )?");

You would then collect all matches in a list as such:

final List<String> matches = new ArrayList<>();

final Matcher m = PATTERN.matcher(input);

while (m.find())
    matches.add(m.group(1));

If there are potential unescaped single quotes then this is not doable with regexes. If they can be escaped, then have a look here for a technique to write an efficient regex.

Community
  • 1
  • 1
fge
  • 119,121
  • 33
  • 254
  • 329