0

I expect the regex (?!(\sI\s)) to match with all the characters in the string HELLO I TEST except the I. But it doesn't seem to work that way. Was trying out here. I expect the pattern to match all characters except the one mentioned in the negation, but it doesn't seems so any help on understanding this would be helpful.

Kannan Ramamoorthy
  • 3,980
  • 9
  • 45
  • 63
  • Split with `/\sI\s/` – Wiktor Stribiżew Jun 08 '20 at 08:28
  • 1
    You are not matching anything with the lookahead assertion. Else try `(?!(I))[A-Z]` – The fourth bird Jun 08 '20 at 08:29
  • Can you clarify how your expectation differs from your result? In the link you posted, we can see a match everywhere except directly in front of " I "? Are you saying that you'd expect the missing match to be in front of the "I" instead of in front of the space (or both)? Or are you saying that you'd expect the matches to actually contain characters rather than just being lines / empty matches? – sepp2k Jun 08 '20 at 08:33
  • @Thefourthbird That works, but when `(?!)` mean anything but, why should I explicitly mention [A-Z]? – Kannan Ramamoorthy Jun 08 '20 at 08:37
  • @sepp2k Yes I'm expecting to match all words except the word ` I ` – Kannan Ramamoorthy Jun 08 '20 at 08:38
  • You can't match anything but a multicharacter string. In PCRE, there is a hack: `(?s)multi-word-pattern(*SKIP)(*F)|(?:(?!multi-word-pattern).)+`. Split with the pattern, and if needed, join the resulting chunks with a string method. – Wiktor Stribiżew Jun 08 '20 at 08:43
  • Splitting would be easier indeed, or `\b(?!I\b)\w+` to match words that are not `I` – The fourth bird Jun 08 '20 at 08:46
  • @KannanRamamoorthy That didn't make it clearer for me. Which of my specific questions did you say "yes" to? Is it about the length of the matches or the position of the missing match? – sepp2k Jun 08 '20 at 08:54

0 Answers0