用逗号(或空格)重复的模式的正则表达式

Vasilis Lourdas:

我想验证一个短语,其中包含由逗号或空格分隔的特定单词的列表。单词列表可以是[A-E]\d(ST|st)\d(作为正则表达式)。因此,一个有效的短语是:

A1, a2, B1, ST1, st3,c3,e4 , st6

虽然A11, G2,stt3不会。

我想出了这个正则表达式:

^([A-E|a-e])?(?(1)\d|(?:ST|st)\d)(?:[\s,]+([A-E|a-e])?(?(2)\d|(ST|st)\d))*$

但这并不总是成功的。虽然b3, st2, a3, st3成功,但b3, st2, a3, st3失败。

有什么帮助吗?

41686d6564:

您可以将以下模式与该i标志一起用于不区分大小写的匹配:

^(?:[A-E]|ST)\d(?:\s*,\s*(?:[A-E]|ST)\d)+$

演示

细节:

^                   # Beginning of the string/line.
(?:[A-E]|ST)        # Match either a single letter between 'A and 'E' or 'ST'.
\d                  # Match a single digit.
(?:                 # Beginning of a non-capturing group.
    \s*,\s*         # Match a single comma surrounded by optional whitespace characters.
    (?:[A-E]|ST)\d  # Either a letter between 'A and 'E' or 'ST' followed by a digit.
)                   # End of the non-capturing group.
+                   # Match the previous group between one and unlimited times.
$                   # End of the string/line.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章