开发者

Regular Expression Conditional Lookahead/Lookback?

开发者 https://www.devze.com 2022-12-11 09:25 出处:网络
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.

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

精彩评论

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