Regular Expression Postive Lookahead substring
I am fairly new to regular expressions and the more and more I use them, the more I like them. I am working on a regular expression that must meet the following conditions:
- Must start with an Alpha character
- Out of the next three characters, at least one must be an Alpha character. 开发者_开发百科
- Anything after the first four characters is an automatic match.
I currently have the following regex: ^[a-zA-Z](?=.*[a-zA-Z]).{1}.*$
The issue I am running into is that my positive lookahead (?=.*[a-zA-Z]).{1}
is not constrained to the next three characters following the alpha character.
I feel as if I am missing a concept here. What am I missing from this expression?
Thanks all.
The .*
in your lookahead is doing that. You should limit the range here like
^[a-zA-Z](?=.{0,2}[a-zA-Z]).{1}.*$
Edit: If you want to make sure, that there are a least 4 characters in the string, you could use another lookahead like this:
^[a-zA-Z](?=.{3})(?=.{0,2}[a-zA-Z]).{1}.*$
What do you want lookahead for? Why not just use
^[a-zA-Z](..[a-zA-Z]|.[a-zA-Z].|[a-zA-Z]..)
and be happy?
You'll probably have to do a workaround. Something like:
^[a-z](?=([a-z]..|.[a-z].|..[a-z])).{3}.*
- First char [a-z]
- Positive lookahead, either first, or second, or third char is a-z
([a-z]..|.[a-z].|..[a-z])
- Other stuff
Change the *
in your lookahead to ?
to get m/^[a-zA-Z](?=.?[a-zA-Z]).{1}.*$
If I am understanding your criteria, that fixes it because of the change in greediness.
These are correctly matched:
a2a3-match
2aaa-no match
Aaaa-match
a333-no match
精彩评论