Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试创建一个正则表达式来匹配单词和空格:
"Hello There" - Match "#Hello" - No Match "123Hello" - No Match
它应该是这样的:
( *[a-zA-Z]* *)
我需要定义一个方法,该方法starts_with_consonant?(s)接受一个字符串,如果它以辅音开头,则返回 true,否则返回 false。
starts_with_consonant?(s)
就我们的目的而言,辅音是除“A”、“E”、“I”、“O”、“U”之外的任何字母。
这是一个课程。
这似乎有效:
(?<=^|\s)[a-zA-Z]+(\s+[a-zA-Z]+)*(?=\s|$)
使用您的示例(和其他示例)查看此操作的现场演示
如果您只想在整个输入匹配时才匹配,请将环视交换为^and $:
^
$
^[a-zA-Z]+(\s+[a-zA-Z]+)*$
看一个现场演示。
我想这几乎可以满足您的要求:
\A(\w*\s+\w+)+\Z