我需要为不以点开头的单词创建正则表达式,它可能包含任何字母、空格和点。
例如:sample,sample.test,sample test
正则表达式不应允许 .sample,sample.,sample .test
如何为此生成正则表达式?
这个正则表达式:^[^.][\p{L} .]+$
应该匹配你所追求的。
这^
是一个锚点,它将指示正则表达式引擎从字符串的最开头开始匹配。[^.]
将匹配任何 1 个不是句点 ( .
) 的字符。[\p{L} .]+
将匹配一个或多个字符,这些字符可以是字母(在此处显示的任何语言中)、空格或句点。最后,$
将指示正则表达式在字符串末尾终止匹配。
编辑:根据您的评论问题,类似的东西应该是可测试的:^[^.][a-zA-Z .]+$
。
用这个
\b\p{L}[\p{L}\s.]*\b
解释
@"
\b # Assert position at a word boundary
\p{L} # A character with the Unicode property “letter” (any kind of letter from any language)
[\p{L}\s.] # Match a single character present in the list below
# A character with the Unicode property “letter” (any kind of letter from any language)
# A whitespace character (spaces, tabs, and line breaks)
# The character “.”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\b # Assert position at a word boundary
"