tags:

views:

83

answers:

3
+1  Q: 

Regular Expression

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 Thank you for your help

+5  A: 

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).*
Gumbo
no, still not there.Actually, all what I want is the negation of ^Ju
@user385411: Well, it works for me (assuming a flag to use case insensitive matching).
Gumbo
`/^(?!ju)[a-z]+/i` - It works for me, too.
Alan Moore
A: 

Could you please tell us, which language you are using?

For example if you need the PCRE for Apache's mod_rewrite, you could simple prepend ! to the PCRE to negate it.

In PHP on the other hand you would use a negative lookahead assertion as Gumbo described.

nikic
it's a jquery script in an ASP page.I have a list and I want to hide all elements that does not start with 'Ju'I honestly thought it will be obvious with regex
A: 

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.

Kilian Foth
what would be the best thing to do then.I have a list and I want to hide all elements that does not start with 'Ju'I am using JQuery
I have to find then the negation of match in JQuery
Use the `:not` operator: http://api.jquery.com/not-selector/
Kilian Foth