0

The rules of the regex are simple:

  • Split by NOT: a whitespace " " followed by 'OR' or '|' :

Example:

"thing to say" france (true)

"thing to say" OR thing (false)

"thing to say" | thing (false)

I'm trying to find a regex to help me do that (with a pregs_split)

I only could do something like /\||OR| \|| OR| \| | OR |\| | OR/ really basic but I need a regex for the opposite case.

Alucard
  • 1,814
  • 1
  • 21
  • 33

1 Answers1

5

Use a negative lookahead

"thing to say" ?(?!\||OR)

EDIT: your comment split by a space followed by "OR" or "|" is different than what I understood your question to be. In that case it would actually make sense to use a positive lookahead:

/ (?=\||OR)/
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • you mean something like `$matches = preg_split('?(?!\||OR)', $stringToSplit);` – Alucard Apr 29 '13 at 14:35
  • Well your question was a bit unclear .. I thought you were just trying to match *not* that regex. You can't split by the lookahead because it does not consume anything. You need a more specific regex. If you just split by a space it splits every word – Explosion Pills Apr 29 '13 at 14:38
  • yeah exaclty, I need to split by a space followed by "OR" or "|" that's why im a little bit confused – Alucard Apr 29 '13 at 14:40
  • Your edit works as a charm, but what if I wanted to find the opposit of that ? I mean find the case when there is a space but not followed by 'OR' or '|' – Alucard Apr 29 '13 at 14:44
  • @vladimire In that case use what you had `/ (?!\||OR)/`, but that is a *lot* of spaces, isn't it? – Explosion Pills Apr 29 '13 at 14:45
  • Yes, it means my rules are no right for this kind of problem. Hum, let me explain it one more time, you may be able to help me: - `"thing to say" word (true)` - `"thing to say" OR word (false)` - `"thing to say" | word (false)` How would a regex look like if I want it to return true in the first case ? I think we should add an other whitespace to the rule ? – Alucard Apr 29 '13 at 14:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29124/discussion-between-explosion-pills-and-vladimire) – Explosion Pills Apr 29 '13 at 14:58