正则表达式条件前瞻/回顾?

发布于 2024-08-10 19:08:06 字数 326 浏览 4 评论 0原文

嘿伙计们,我想问一下你们是否可以使用前瞻或任何其他机制对单个正则表达式进行一些条件检查。

例如,在我的正则表达式中,如果前一个值超过 3,我希望下一个值的范围为 0-5;如果前一个值低于 3,则下一个值的范围为 0-9。

例如:

[0-9] 下一个匹配应该是 [0 -5] 或 [0-9] 取决于先前的值是低于还是高于 5。

代码如下所示:

调用此 A--> [0-9][0-9]<-- 将此称为 B

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

这可以作为单个正则表达式吗?

Hey guys I wanted to ask if you can 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-08-17 19:08:06

这是正向前瞻的格式:

/(?=expression)/

这是负向前瞻:

/(?!expression)/

编辑

对于您的示例,这意味着如下所示:

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

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]+)/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文