Match regex expression Regex sketchengine (doesn't match substring)

Viewed 144

I am learning about regular expressions and I'm trying to solve this question: https://regex.sketchengine.co.uk/cgi/ex1.cgi

So far, I've come up with:

^[psr][^ta|?!ea].*$

But instead of checking if it doesn't match 'ea' as a substring, it tries to not match 'e' and 'a' as a second character. What is my error in this?

2 Answers

Your regex is wrong, see its description:

NODE EXPLANATION
^ the beginning of the string
[psr] any character of: 'p', 's', 'r'
[^ta|?!ea] any character except: 't', 'a', '
.* any character except \n (0 or more times (matching the most amount possible))
$ before an optional \n, and the end of the string

Use

.*p[ioa ]t.*

See proof

EXPLANATION

--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  p                        'p'
--------------------------------------------------------------------------------
  [ioa ]                   any character of: 'i', 'o', 'a', ' '
--------------------------------------------------------------------------------
  t                        't'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))

In your pattern you do not allow matching pe se and re ruling out respite, but you only don't want to allow pe.

You could use a negative lookahead to rule out a p directly followed by one of your characters in the character class.

^(?!p[tea])[psr].*

The pattern matches:

  • ^ Start of the string
  • (?!p[tea]) Negative lookahead, assert not pt or pe or pa directly to the right
  • [psr].* Match either p s r followed by 0+ times any character

Regex demo

Note that there are no | ? or ! in the example data.

Related