正则表达式正好 n 或 m 次 [英] Regex exactly n OR m times

查看:23
本文介绍了正则表达式正好 n 或 m 次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下正则表达式,其中 Xany 正则表达式.

Consider the following regular expression, where X is any regex.

X{n}|X{m}

这个正则表达式将测试 X 出现 完全 nm 次.

This regex would test for X occurring exactly n or m times.

是否有一个正则表达式量词可以测试 X 恰好 nm 次出现?

Is there a regex quantifier that can test for an occurrence X exactly n or m times?

推荐答案

没有单一的量词表示恰好 m 次或 n 次".你这样做的方式很好.

There is no single quantifier that means "exactly m or n times". The way you are doing it is fine.

另一种选择是:

X{m}(X{k})?

其中 m kn-m 的值.

where m < n and k is the value of n-m.

这篇关于正则表达式正好 n 或 m 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆