[英]Match the following regex: #*:*#
I would like to find if a string contains a regular expression that starts and ends with a hash (#) and contains colon (:). 我想找到一个字符串是否包含一个以哈希(#)开头和结尾并包含冒号(:)的正则表达式。 All those should be matched on the same line
所有这些都应该在同一条线上匹配
Example: hello to #some:test# and george MATCH 示例:你好#some:test#和george MATCH
Example: hello to #:# world NO MATCH 示例:你好#:#world NO MATCH
Example: hello to #test:# world NO MATCH 示例:你好#test:#world NO MATCH
Example: hello #:test# world NO MATCH 示例: hello#:test#world NO MATCH
Example: 例:
hello [newline] 你好[换行]
to #some:test# world NO MATCH #some:test#world NO MATCH
Thanks, Yannis 谢谢,Yannis
This should do: 这应该做:
@"\#.+:.+\#"
Breakdown: 分解:
\# - Match a # character
.+ - Followed by one or more characters
: - Followed by a : character
.+ - Followed by one or more characters
\# - Followed by a # character
Note that .
请注意
.
will match any character and is probably not the most efficient regular expression. 将匹配任何字符,可能不是最有效的正则表达式。
A non greedy version (more efficient): 非贪婪版本(更高效):
@"\#[^:]+:[^#]+\#"
\# - 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.