Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to find values that don't start with a particular character sequence

How to write a regular expression for something that does NOT start with a given word

Let's suppose I have the following list

  • January
  • February
  • June
  • July
  • October

I want a regular expression that returns all but June and July because they they start with Ju

I wrote something like this ^[^ju] but this return any starting with J or U I need something that starts with Ju

like image 327
user385411 Avatar asked Dec 17 '22 23:12

user385411


1 Answers

Try this regular expression:

^([^j].*|j($|[^u].*))

This matches strings that either do not start with j, or if they start with j there is not a u at the second position.

If you can use look-around assertions, you can also use this:

^(?!ju).*
like image 128
Gumbo Avatar answered May 22 '23 23:05

Gumbo