带有 +(加号) 和空格模式的正则表达式
我的正则表达式有以下句子
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
*
(空格、星号)允许可选空格。您可能还需要考虑将任何空格与
\s
(例如,包括制表符)匹配,而不仅仅是空格。要允许数字或字母,请使用字符类,例如
[0o]
。Use
*
(space, asterisk) to allow optional spaces.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]
.如果您想匹配空格,那么您需要将其包含在正则表达式中。
\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 morer
characters but will NOT look for whitespace.r+\s*
with match one or morer
characters followed by zero or more whitespace characters.You can build the rest of your expression from there.
这匹配每个字母,后跟零个或多个相同的字母或空格
以及您指定的数字...(e = 3,o = 0,s = 5)
This matches each letter, followed by zero or more of the same letter or space
With numbers as you specified... (e = 3, o = 0, s = 5)
更新2:
根据您问题中的信息,您可以使用(已更新以捕获蒂姆·皮茨克(Tim Pietzcker)指出的情况!):
我刚刚意识到这使得答案与马克·拜尔斯(Mark Byers)相同!
UPDATED 2:
Based on the info in your question, you can use (updated to catch the cases that Tim Pietzcker pointed out!):
I just realized that this makes the answer the same as Mark Byers, though!