开发者

Regular Expression Conditional Lookahead/Lookback?

Hey guys I wanted to ask if you ca开发者_StackOverflow社区n do some conditional checks on a single regular expression using lookahead or any other mechanism.

For example in my regex I want to the next value to range from 0-5 if the previous was over 3 or range from 0-9 if the previous was under 3.

Eg:

[0-9] next match should be either [0-5] OR [0-9] depending on whether the previous value was under or over 5.

as code think of it like this:

calls this A--> [0-9][0-9]<-- call this B

if (A < 5) then B [0-9] Else B [0-5]

Is this possible as a single regular expression?


This is the format for a positive lookahead:

/(?=expression)/

And this is the negative lookahead:

/(?!expression)/

EDIT

For your example, this would mean something like this:

/((?=[5-9]+)[0-5]+)|((?=[0-4]+)[0-9]+)/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜