0

我想查找一个字符串是否包含一个以哈希 (#) 开头和结尾并包含冒号 (:) 的正则表达式。所有这些都应该在同一行匹配

示例:你好 #some:test# 和 George MATCH

示例: hello to #:# world NO MATCH

示例: hello to #test:# world NO MATCH

示例:你好 #:test# world NO MATCH

例子:

你好[换行符]

到 #some:test# world NO MATCH

谢谢, 雅尼斯

4

1 回答 1

2

这应该这样做:

@"\#.+:.+\#"

分解:

\#   - Match a # character
.+   - Followed by one or more characters
:    - Followed by a : character
.+   - Followed by one or more characters
\#   - Followed by a # character

请注意,.它将匹配任何字符,并且可能不是最有效的正则表达式。


非贪婪版本(更高效):

@"\#[^:]+:[^#]+\#"

\#    - Match a # character
[^:]+ - Followed by one or more characters that are not :
:     - Followed by a : character
[^#]+ - Followed by one or more characters that are not #
\#    - Followed by a # character
于 2012-09-02T10:26:21.227 回答