简体   繁体   English

如何在n次构造中强制匹配另一个字符串重复n次

[英]How to force matching a different string in an OR construct repeated n times

I am new to regex, i am trying to validate a field the user is entering weekend days in a comma separated format and using 2 characters abbreviation for the day. 我是regex的新手,我正在尝试验证用户输入的字段是否以逗号分隔的格式输入周末,并且一天中使用2个字符的缩写。 i developed the following pattern that is not working as i want it to: 我开发了以下无法正常运行的模式:

^(fr|sa|su|mo|tu|we|th)?(?(1)(,fr|,sa|,su|,mo|,tu|,we|,th)){0,5}$ ^(fr | sa | su | mo | tu | we | th)?(?(1)(,fr |,sa |,su |,mo |,tu |,we |,th)){0,5} $

This pattern successfully matches the desired input like the following: 此模式成功匹配所需的输入,如下所示:

fr fr

mo

fr,sa fr,sa

fr,tu fr,tu

su,mo,tu,we,fr,sa su,mo,tu,我们,fr,sa

but it also matches the following wrong enteries: 但它也匹配以下错误的输入:

fr,fr,fr,fr fr,fr,fr,fr

sa,sa,sa,sa sa,sa,sa,sa

I want a way to force the second group to repeat only different values included in the OR construct. 我想要一种强制第二组仅重复OR构造中包含的不同值的方法。 is there a way regex can do that? 正则表达式可以做到这一点吗?

You may use a (?!.*([az]{2}).*\\1) negative lookahead at the start of regex to disallow repeating 2-letter values in the string (due to the string format, you do not even need word boundaries or comma context checks in the lookahead): 您可以在正则表达式的开头使用(?!.*([az]{2}).*\\1)负前瞻,以禁止在字符串中重复2个字母的值(由于字符串格式,甚至不需要在前瞻中进行单词边界或逗号上下文检查):

^(?!.*\b([a-z]{2})\b.*\b\1\b)(fr|sa|su|mo|tu|we|th)?(?:,(?:fr|sa|su|mo|tu|we|th)){0,5}$

See the regex demo . 参见regex演示

The (?!.*\\b([az]{2})\\b.*\\b\\1\\b) is a negative lookahead that fails the match if there is a duplicate two-letter chunk in the string. (?!.*\\b([az]{2})\\b.*\\b\\1\\b)是否定的超前行为,如果字符串中有重复的两个字母的块,则匹配失败。

Java demo : Java演示

String day = "fr|sa|su|mo|tu|we|th";
String pattern = "(?!.*\\b([a-z]{2})\\b.*\\b\\1\\b)(?:" + day + ")?(?:,(?:" + day + ")){0,5}";
if (s.matches(pattern)) {
    System.out.println("Valid!");
} else {
    System.out.println("Invalid!");
}

Note that String#matches requires a full string match, so ^ and $ are not required. 请注意, String#matches需要完整的字符串匹配,因此不需要^$

Note you may shorten the day part using character classes , fr|sa|su|mo|tu|we|th => fr|s[au]|mo|t[uh]|we . 请注意,您可以使用字符类fr|sa|su|mo|tu|we|th => fr|s[au]|mo|t[uh]|we来缩短day部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM