我需要一个与以下Hello,World,Hello World匹配的正则表达式模式

时间:2015-05-21 14:54:58

标签: regex

我需要一个与以下“Hello”,“World”,“Hello World”,“HelloWorld”,“Hello / World”相匹配的正则表达式模式

3 个答案:

答案 0 :(得分:3)

如果问题没有准确回答,很难。您应该提供更好的问题以获得更好的答案。

这很简单:

(Hello)?\s?(World)?

Regular expression visualization

如您所见,此模式由多个可选模式组成,因此它也可以匹配空字符串。所以,如果你想要至少有一个模式匹配,你可以这样做:

Hello\s?World|Hello|World

Regular expression visualization

答案 1 :(得分:0)

这是关于你可以得到的最精简:

Hello(?:[ /]?World)?|World

展开:

   Hello (?: [ /]? World )?
|  World

答案 2 :(得分:0)

嗯,这有点棘手,但我成功完成了它。所以我和你分享我的正则表达,我希望它可以提供帮助:

(?m)^(?(?=\110\x65\x6c{2}\157)(?:\110\x65\x6c{2}(?P<o>\157)[\x20\x2f]?\127\k'o'\x72\x6c\x64|\110\x65\x6c{2}\157$)|\127\157\x72\x6c\x64$)

Here is an online demo

说明:

(?m)    # Enabling multi-line (For me only to show you how it works on input strings)
^   # Beginning of line
(?  # Beginning an IF condition
    (?=\110\x65\x6c{2}\157) # Positive Lookahead. IF '\110\x65\x6c{2}\157' was found...
    (?: # Start of a non-capturing group
        \110\x65\x6c{2}(?P<o>\157)[\x20\x2f]?\127\k'o'\x72\x6c\x64  # Then match '\110\x65\x6c{2}(?P<o>\157)[\x20\x2f]?\127\k'o'\x72\x6c\x64'
        |   # Otherwise
        \110\x65\x6c{2}\157 # Match '\110\x65\x6c{2}\157'
        $   # End of line
    )   # End of non-capturing group
|   # IF condition else statement
    \127\157\x72\x6c\x64    # Match '\127\157\x72\x6c\x64'
    $   # End of line
)   # End of IF condition

你可能会问它是否真的有用,我很高兴能说。你没想到像这样的 Hello World ?然后欢迎程序员 01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100!

- 顺便说一下,这是一个很好的问题。