如果字符串恰好包含 N 个字符,则匹配正则表达式

发布于 2025-01-05 16:23:40 字数 170 浏览 1 评论 0原文

我希望正则表达式仅在包含出现预定义次数的字符时才匹配字符串。

例如: 我想匹配所有包含字符“_”的字符串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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一梦浮鱼 2025-01-12 16:23:40

对于您的示例,您可以这样做:(

\b[a-z]*(_[a-z]*){3}[a-z]*\b

带有忽略大小写标志)。

你可以在这里玩它,

它说“匹配0个或多个字母,后跟'_[az]*'”三次,后跟 0 个或多个字母”。 \b 表示“单词边界”,即“匹配整个单词”。

由于我使用了“*”,如果单词中正好有三个“_”,无论它出现在单词的开头还是结尾,这都会匹配 - 您可以以其他方式修改它。

另外,我假设您想要匹配字符串中恰好包含三个“_”的所有单词。

这意味着字符串“a_b a_b_c_d”会表示“a_b_c_d”通过(但“a_b”失败)。

如果您的意思是全局整个字符串中您只希望出现三个“_”,则使用:

^[^_]*(_[^_]*){3}[^_]*$

这将正则表达式锚定在字符串的开头并转到最后,确保其中仅出现 3 次“_”。

For your example, you could do:

\b[a-z]*(_[a-z]*){3}[a-z]*\b

(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:

^[^_]*(_[^_]*){3}[^_]*$

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.

执笔绘流年 2025-01-12 16:23:40

详细阐述 Rado 的答案,这是迄今为止最多价的,但如果有更多匹配项,可能会很痛苦:

^([^_]*_){3}[^_]*$

它将匹配整个字符串(从开头 ^ 到结尾 $),其中恰好有 3 ({3}) 次由 0 或更多 (*) 次非下划线字符组成的模式([^_]) 和一个下划线 (_),整个后跟除下划线以外的任意字符 0 次以上 ([^_]*再次)。

当然,也可以相反地分组,就像在我们的例子中,模式是对称的:

^[^_]*(_[^_]*){3}$

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 :

^([^_]*_){3}[^_]*$

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 :

^[^_]*(_[^_]*){3}$
も星光 2025-01-12 16:23:40

这应该可以做到:

^[^_]*_[^_]*_[^_]*_[^_]*$

This should do it:

^[^_]*_[^_]*_[^_]*_[^_]*$
惟欲睡 2025-01-12 16:23:40

如果你的例子是唯一的可能性(比如 a_b_c_...),那么其他的都很好,但我写了一个可以处理其他一些可能性的例子。如:

a__b_adf
a_b_asfdasdfasfdasdfasf_asdfasfd
___
_a_b_b

等等。

这是我的正则表达式。

\b(_[^_]*|[^_]*_|_){3}\b

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:

a__b_adf
a_b_asfdasdfasfdasdfasf_asdfasfd
___
_a_b_b

Etc.

Here's my regex.

\b(_[^_]*|[^_]*_|_){3}\b
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文