208_man
208_man

Reputation: 1728

Regex that matches strings that are all lower case and do not contain specific string

I need a regular expression to ensure that entries in a form 1) are all lower case AND 2) do not contain the string ".net"

I can do either of those separately:

^((?!.net).)*$ gives me strings that do not contain .net. [a-z] only matches lower-cased inputs. But I have not been able to combine these.

I've tried:

^((?!.net).)(?=[a-z])*$ (^((?!.net).)*$)([a-z])

And a few others.

Can anyone spot my error? Thanks!

Upvotes: 1

Views: 1230

Answers (1)

The fourth bird
The fourth bird

Reputation: 163217

As you are using a dot in your pattern that would match any char except a newline, you can use a negated character class to exclude matching uppercase chars or a newline.

As suggested by @Wiktor Stribiżew, to rule out a string that contains .net you can use a negative lookahead (?!.*\.net) where the .net (note to escape the dot) is preceded by .* to match 0+ times any character.

^(?!.*\.net)[^\nA-Z]+$
  • ^ Start of string
  • (?!.*\.net) negative lookahead to make sure the string does not contain .net
  • [^\nA-Z]+ Match 1+ times any character except a newline or a char A-Z
  • $ End of string

Regex demo

Upvotes: 3

Related Questions