Reputation: 1317
I've got a couple of strings, usually words and letter, upper and lower case, mixed with numbers. I'd like to match them all unless there are two consecutive underscores in the string. Examples of my strings:
first-string-String-71-word
second-otherword-X-17-word-last-word
third-nextword-8-word__otherword
Now I'm searching for a regex which matches the first and second but not the third.
([a-zA-Z0-9-]+)
(?!__)([a-zA-Z0-9-]+)
([a-zA-Z0-9-^(__)]+)
Those seem to be all not working - what's the right approach here?
https://regex101.com/r/i7AnSS/1
Upvotes: 2
Views: 75
Reputation: 147196
You can add a negative lookahead for two consecutive _
in the string. Note that you need to anchor the regex to the start and end of the string, otherwise it can match from partway through.
^(?!.*?__)([a-zA-Z0-9_-]+)$
https://regex101.com/r/i7AnSS/3
Note that this assumes your strings are allowed to have a single _
in them. If that is not the case, the simple
^([a-zA-Z0-9-]+)$
will exclude any strings with _
in them.
Upvotes: 1
Reputation: 101
Use it
^([a-zA-Z0-9-]+)$
and Check it on Url https://regex101.com/r/u82mre/1
Upvotes: 0