Regex - non-contiguous range of repetitions

一个人想着一个人 提交于 2021-02-11 13:24:35

问题


I am trying to build a regex that matches a pattern a certain number of times, e.g. 3 or 5. [a-z]{3,5} will match [a-z] 3, 4 or 5 times, but I don't want the 4.

I know I could do something like ([a-z]{3})([a-z]{2})?, but that means that for cases where I want to match the pattern 3, 5, 7, 13 or 29 times, the resulting regex would be particularly nasty.

Is there any better way to do this?

(I used [a-z] as an example, but it could be anything else)


回答1:


Regular expressions don't support an arbitrary "match exactly X, Y, or Z times".
That said, you could use something like ([a-z]{29}|[a-z]{3}|[a-z]{3}) listing the match counts as options. However, if your pattern is more complicated than just [a-z], things may get messy.

A more pragmatic solution would be to count the number of times the pattern matches, then keep the result if it's in your list of acceptable repetitions. In pseudocode, that would look like:

acceptable_match_counts = [1, 3, 5, 7, 19, 32, 99];
num_matches = regex(/([a-z]){min,max}/, my_data);

if (num_matches in acceptable_match_counts) {
    keep_match
} else {
    reject_match
}

However, it's not a pure regex solution.



来源:https://stackoverflow.com/questions/37351490/regex-non-contiguous-range-of-repetitions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!