不以点开头的单词的正则表达式

时间:2012-06-05 09:54:17

标签: regex c#-4.0

我需要为不以点开头的单词创建正则表达式,它可能包含任何字母,空格和点。

Ex:样品,样品,样品,样品测试

正则表达式不应该允许.sample,sample。,sample .test

如何为此生成正则表达式?

2 个答案:

答案 0 :(得分:1)

这个正则表达式:^[^.][\p{L} .]+$应该与你所追求的相匹配。

^是一个锚点,它将指示正则表达式引擎从字符串的最开头开始匹配。 [^.]将匹配任何不是句点(.)的1个字符。 [\p{L} .]+将匹配一个或多个字符,这些字符可以是字母(使用任何语言显示here),空格或句点。最后,$将指示正则表达式在字符串末尾终止匹配。

编辑:根据您的评论问题,类似的内容应该是可测试的:^[^.][a-zA-Z .]+$

答案 1 :(得分:0)

使用此

\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
"