0

How to mark xy if the next symbol is not y?

I have four strings: 1. zxyy 2. zxyz 3. zxy 4. xy The epxression should mark strings 2-4. This regex marks the 2-nd string only: ([x][y])(?=[^y]) Thanx.

SharpUser
  • 97
  • 8
  • 1
    Use a negative lookahead instead of a positive lookahead using a negated class. This way you don't need to match a character that isn't `y`, you just need not to match `y` – Aaron Feb 26 '19 at 15:44
  • @Aaron, I do not understand, sorry. – SharpUser Feb 26 '19 at 15:48
  • 2
    Your regex says "match xy if it's followed by something that isn't y". It should instead say "match xy if it's not followed by y", which would work even when xy isn't followed by anything. You're currently using a positive lookahead (`(?=...)`) and should be using a negative lookahead (`(?!...)`) instead. – Aaron Feb 26 '19 at 15:49
  • 2
    As a side note there's no reason to put the x and y characters in character classes, `xy` will match the same things as `[x][y]` and is more readable and easier to write. – Aaron Feb 26 '19 at 15:51
  • @Aaron, if I understand correctly, I should use `([xy])(?![^y])` But it does not work as I expected. – SharpUser Feb 26 '19 at 15:56
  • try reading you regexp in english - currently you have: x or y not followed by not y. Is that what you mean? – jhnc Feb 26 '19 at 15:57
  • perhaps you want: x followed by y not followed by y – jhnc Feb 26 '19 at 15:59
  • @jhnc, yes, that is what I mean. But it does not work if there is nothing after `xy`. – SharpUser Feb 26 '19 at 16:01
  • try to convert the english into regexp: to match x, you write `x`. to match x followed by y, you write `xy`. to match "not followed by y" what do you need? – jhnc Feb 26 '19 at 16:02
  • 1
    @jhnc, ok, this `(x)(y)(?!y)` works, thanx! – SharpUser Feb 26 '19 at 16:06
  • you got it. now write it up as an answer so it will help someone else :-) – jhnc Feb 26 '19 at 16:07
  • 1
    `xy(?!y)` would likely be enough by the way. `(capturing groups)` are useful when you want to extract a part of what you're matching, but here the content of your two groups will always be "x" and "y". – Aaron Feb 26 '19 at 16:09
  • @Aaron, you're right, `xy(?!y)` is enough, thanx! – SharpUser Feb 26 '19 at 16:14

1 Answers1

1

The regex recommended by Aaron works as I wished:

xy(?!y)

It marks 2. zxyz 3. zxy 4. xy, but not 1. zxyy.

SharpUser
  • 97
  • 8