开发者

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:

  1. Must start with an Alpha character
  2. Out of the next three characters, at least one must be an Alpha character.
  3. 开发者_开发百科
  4. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜