문제

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?

Example

Is there a way to shorten the following:

[acegikmoqstz@#&].*[acegikmoqstz@#&].*[acegikmoqstz@#&]

도움이 되었습니까?

해결책

Keep in mind that regex features are dependant on the language being used.

With Java, you can do this:

[acegikmoqstz@#&](?:.*[acegikmoqstz@#&]){2}

But that's all, with java you can't refer to named subpattern.

With PHP you can do that:

(?(DEFINE)(?<a>[acegikmoqstz@#&]))\g<a>(?:.*\g<a>){2}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top