匹配以下正则表达式:#*:*#

时间:2012-09-02 10:23:33

标签: c# regex

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

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

示例:你好#:#world NO MATCH

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

示例:你好#:test#world 没有比赛

示例:

你好[换行符]

到#some:test#world NO MATCH

谢谢, 雅尼斯

1 个答案:

答案 0 :(得分: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