本文介绍了在正则表达式中重用字符类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了使正则表达式更简短,是否有一种简便的方法来引用同一正则表达式中较早出现的字符类?
In order to keep a regular expression more brief, is there a shorthand way to refer to a character class that occurs earlier in the same regular expression?
示例
有没有一种方法可以缩短以下时间:
Is there a way to shorten the following:
[acegikmoqstz@#&].*[acegikmoqstz@#&].*[acegikmoqstz@#&]
推荐答案
请记住,正则表达式功能取决于所使用的语言.
Keep in mind that regex features are dependant on the language being used.
使用Java,您可以执行以下操作:
With Java, you can do this:
[acegikmoqstz@#&](?:.*[acegikmoqstz@#&]){2}
仅此而已,使用Java不能引用命名子模式.
But that's all, with java you can't refer to named subpattern.
使用PHP可以做到:
(?(DEFINE)(?<a>[acegikmoqstz@#&]))\g<a>(?:.*\g<a>){2}
这篇关于在正则表达式中重用字符类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!