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 som开发者_如何学Cething that starts with Ju
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).*
It's a bad idea to use regular expressions for complement matches. It works, but it is usually either really inefficient or engine-specific. Use a regex and combine it with the not
operator instead.
Below regex will also work for words starting with ju,
^[j][u].*
精彩评论