如果字符串恰好包含 N 个字符,则匹配正则表达式
我希望正则表达式仅在包含出现预定义次数的字符时才匹配字符串。
例如: 我想匹配所有包含字符“_”的字符串3次;
所以 “a_b_c_d”会通过
“a_b”会失败
“a_b_c_d_e”会失败
有人知道一个简单的正则表达式可以满足这个要求吗?
谢谢
I'd like a regular expression to match a string only if it contains a character that occurs a predefined number of times.
For example:
I want to match all strings that contain the character "_" 3 times;
So
"a_b_c_d" would pass
"a_b" would fail
"a_b_c_d_e" would fail
Does someone know a simple regular expression that would satisfy this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您的示例,您可以这样做:(
带有忽略大小写标志)。
你可以在这里玩它,
它说“匹配0个或多个字母,后跟'_[az]*'”三次,后跟 0 个或多个字母”。
\b
表示“单词边界”,即“匹配整个单词”。由于我使用了“*”,如果单词中正好有三个“_”,无论它出现在单词的开头还是结尾,这都会匹配 - 您可以以其他方式修改它。
另外,我假设您想要匹配字符串中恰好包含三个“_”的所有单词。
这意味着字符串“a_b a_b_c_d”会表示“a_b_c_d”通过(但“a_b”失败)。
如果您的意思是全局在整个字符串中您只希望出现三个“_”,则使用:
这将正则表达式锚定在字符串的开头并转到最后,确保其中仅出现 3 次“_”。
For your example, you could do:
(with an ignore case flag).
You can play with it here
It says "match 0 or more letters, followed by '_[a-z]*' exactly three times, followed by 0 or more letters". The
\b
means "word boundary", ie "match a whole word".Since I've used '*' this will match if there are exactly three "_" in the word regardless of whether it appears at the start or end of the word - you can modify it otherwise.
Also, I've assumed you want to match all words in a string with exactly three "_" in it.
That means the string "a_b a_b_c_d" would say that "a_b_c_d" passed (but "a_b" fails).
If you mean that globally across the entire string you only want three "_" to appear, then use:
This anchors the regex at the start of the string and goes to the end, making sure there are only three occurences of "_" in it.
详细阐述 Rado 的答案,这是迄今为止最多价的,但如果有更多匹配项,可能会很痛苦:
它将匹配整个字符串(从开头
^
到结尾$
),其中恰好有 3 ({3}
) 次由 0 或更多 (*
) 次非下划线字符组成的模式([^_]
) 和一个下划线 (_
),整个后跟除下划线以外的任意字符 0 次以上 ([^_]*再次)。
当然,也可以相反地分组,就像在我们的例子中,模式是对称的:
Elaborating on Rado's answer, which is so far the most polyvalent but could be a pain to write if there are more occurrences to match :
It will match entire strings (from the beginning
^
to the end$
) in which there are exactly 3 ({3}
) times the pattern consisting of 0 or more (*
) times any character not being underscore ([^_]
) and one underscore (_
), the whole being followed by 0 ore more times any character other than underscore ([^_]*
, again).Of course one could alternatively group the other way round, as in our case the pattern is symmetric :
这应该可以做到:
This should do it:
如果你的例子是唯一的可能性(比如 a_b_c_...),那么其他的都很好,但我写了一个可以处理其他一些可能性的例子。如:
等等。
这是我的正则表达式。
If you're examples are the only possibilities (like a_b_c_...), then the others are fine, but I wrote one that will handle some other possibilities. Such as:
Etc.
Here's my regex.