简体   繁体   中英

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

Example: hello to #:# world NO MATCH

Example: hello to #test:# world NO MATCH

Example: hello #:test# world NO MATCH

Example:

hello [newline]

to #some:test# world NO MATCH

Thanks, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM