I want to make a regex that recognize some patterns and some not.
_*[a-zA-Z][a-zA-Z0-9_][^-]*.*(?<!_)
The sample of patterns that i want to recognize:
a100__version_2
_a100__version2
And the sample of patterns that i dont want to recognize:
100__version_2
a100__version2_
_100__version_2
a100--version-2
The regex works for all of them except this one:
a100--version-2
So I don't want to match the dashes.
I tried _*[a-zA-Z][a-zA-Z0-9_][^-]*.*(?<!_)
so the problem is at [^-]
CodePudding user response:
You could write the pattern like this, but [^-]*
can also match newlines and spaces.
To not match newlines and spaces, and matching at least 2 characters:
^_*[a-zA-Z][a-zA-Z0-9_][^-\s]*$(?<!_)
Or matching only word characters, matching at least a single character repeating \w*
zero or more times:
^_*[a-zA-Z]\w*$(?<!_)
^
Start of string_*
Match optional underscores[a-zA-Z]
Match a single char a-zA-Z\w*
Match optional word chars (Or[a-zA-Z0-9_]*
)$
End of string(?<!_)
Assert not_
to the left at the end of the string
CodePudding user response:
To exclude dashes from the regex, you can use the negative lookahead assertion (?!-) after the [^-] character class. This will make sure that the regex does not match any dashes after the [^-] character class.
Here's an updated version of the regex that excludes dashes:
[a-zA-Z]a-zA-Z0-9_.*(?<!)
This regex should match the patterns you want to recognize, and exclude the patterns you don't want to recognize.
Here are some examples of how this regex will work:
a100__version_2 // matches
_a100__version2 // matches
100__version_2 // does not match
a100__version2_ // does not match
_100__version_2 // does not match
a100--version-2 // does not match