带有 +(加号) 和空格模式的正则表达式

发布于 2024-12-08 03:29:23 字数 566 浏览 0 评论 0原文

我的正则表达式有以下句子

"my hooorrrr sssee strongggg"

"(h+o+r+s+e+) s+t+r+o+n+g+"

只有当句子为:

"my hooorrrrsssee strongggg"

任何机构可以提供帮助时才有效/匹配吗?

我想匹配无论单词之间的空格。

example : "my h ooo rrr rss    se eee    stttro ngggg"

另一个问题是当某些字符被数字替换时,如下所示:

"my h 0o0 rrr rs555sss    se ee333    stttr0 ngggg"

a replaced by 4
b replaced by 8
s replaced by 5
i replaced by 1
o replaced by 0

任何机构都可以提供帮助吗?

谢谢

I have following sentence

"my hooorrrr sssee strongggg"

my regex is :

"(h+o+r+s+e+) s+t+r+o+n+g+"

it's only valid/match when the sentence is :

"my hooorrrrsssee strongggg"

any body can help ?

i want to match no matter the space between word.

example : "my h ooo rrr rss    se eee    stttro ngggg"

and the other problem is when the some character replaced by number, like bellow :

"my h 0o0 rrr rs555sss    se ee333    stttr0 ngggg"

a replaced by 4
b replaced by 8
s replaced by 5
i replaced by 1
o replaced by 0

any body can help ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

若能看破又如何 2024-12-15 03:29:23

使用 *(空格、星号)允许可选空格。

(h[h ]*o[o ]*r[r ]*s[s ]*e[e ]*)s[s ]*t[t ]*r[r ]*o[o ]*n[n ]*g[g ]*

您可能还需要考虑将任何空格与 \s (例如,包括制表符)匹配,而不仅仅是空格。

要允许数字或字母,请使用字符类,例如 [0o]

(h[h ]*[o0][o0 ]*r[r ]*[s5][s5 ]*[e3][e3 ]*)[s5][s5 ]*t[t ]*r[r ]*[o0][o0 ]*n[n ]*g[g ]*

Use * (space, asterisk) to allow optional spaces.

(h[h ]*o[o ]*r[r ]*s[s ]*e[e ]*)s[s ]*t[t ]*r[r ]*o[o ]*n[n ]*g[g ]*

You may also want to consider matching any whitespace with \s (which includes tab, for example) instead of just space.

To allow numbers or letters use a character class such as [0o].

(h[h ]*[o0][o0 ]*r[r ]*[s5][s5 ]*[e3][e3 ]*)[s5][s5 ]*t[t ]*r[r ]*[o0][o0 ]*n[n ]*g[g ]*
风情万种。 2024-12-15 03:29:23

如果您想匹配空格,那么您需要将其包含在正则表达式中。 \s 是匹配空白字符(即空格、制表符、换行符)的模式。

r+ 将匹配一个或多个 r 字符,但不会查找空格。

r+\s* 匹配一个或多个 r 字符,后跟零个或多个空白字符。

您可以从那里构建其余的表达式。

If you want to match whitespace then you need to include that in the regex. \s is the pattern that matches whitespace characters (that's space, tab, newline characters).

r+ will match one or more r characters but will NOT look for whitespace.

r+\s* with match one or more r characters followed by zero or more whitespace characters.

You can build the rest of your expression from there.

稀香 2024-12-15 03:29:23

这匹配每个字母,后跟零个或多个相同的字母或空格

h[h ]*o[o ]*r[r ]*s[s ]*e[e ]*s[s ]*t[t ]*r[r ]*o[o ]*n[n ]*g[g ]*

以及您指定的数字...(e = 3,o = 0,s = 5)

h[h ]*[o0][o 0]*r[r ]*[s5][s 5]*e[e 3]*[s5][s 5]*t[t ]*r[r ]*[o0][o 0]*n[n ]*g[g ]*

This matches each letter, followed by zero or more of the same letter or space

h[h ]*o[o ]*r[r ]*s[s ]*e[e ]*s[s ]*t[t ]*r[r ]*o[o ]*n[n ]*g[g ]*

With numbers as you specified... (e = 3, o = 0, s = 5)

h[h ]*[o0][o 0]*r[r ]*[s5][s 5]*e[e 3]*[s5][s 5]*t[t ]*r[r ]*[o0][o 0]*n[n ]*g[g ]*
堇色安年 2024-12-15 03:29:23

更新2

根据您问题中的信息,您可以使用(已更新以捕获蒂姆·皮茨克(Tim Pietzcker)指出的情况!):

[h][h ]*[o0][o 0]*[r][r ]*[s][s 5]*[e3][e 3]*[s5][s 5]*[t][t ]*[r][r ]*[o0][o 0]*[n][n ]*[g][g ]*

我刚刚意识到这使得答案与马克·拜尔斯(Mark Byers)相同!

UPDATED 2:

Based on the info in your question, you can use (updated to catch the cases that Tim Pietzcker pointed out!):

[h][h ]*[o0][o 0]*[r][r ]*[s][s 5]*[e3][e 3]*[s5][s 5]*[t][t ]*[r][r ]*[o0][o 0]*[n][n ]*[g][g ]*

I just realized that this makes the answer the same as Mark Byers, though!

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