Regex starts with word but exclude if matches word exactly

Viewed 79

I want to take strings that start with syslog but but not syslog exactly

e.g:

  • syslog = not match
  • syslog.1 = match
  • random = not match

So far, I have this to exclude syslog exactly:

(?!^syslog$)(^.*$)
2 Answers

You can match a non whitespace char \S after matching syslog, and then match the rest of the line without using a lookahead.

^syslog\S.*$

Regex demo

Use

^syslog.+

See proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  syslog                   'syslog'
--------------------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
Related