2012-09-02 78 views
0

我想查找一個字符串是否包含以散列(#)開頭和結尾的正則表達式幷包含冒號(:)。所有這些必須在同一行相匹配匹配以下正則表達式:#*:*#

例子:打招呼#some:測試#和喬治MATCH

例子:打招呼#:#世界敵不過

實施例:打招呼#TEST:#世界NO MATCH

實施例:你好#:測試#世界NO MATCH

實施例:

你好[換行符]

到#some:試驗#世界NO MATCH

謝謝, Yannis

+0

[你嘗試過什麼(http://whathaveyoutried.com)? – Oded

回答

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 
+0

你可能想要使正則表達式非貪婪。 – knittl

+0

謝謝Oded。那我怎麼能讓這個效率更高一點呢? – Yannis

+0

@Yannis - 答案更新。 – Oded