开发者

Regex expression to match all instances of string

Using System.Text.RegularExpressions with the following expression to match all tokens wrapped with # that contain only text (no whitespace etc)

#([a-zA-Z]+)#

and the following test string

text #test# text #test1# text

I only get one match开发者_运维技巧. What am I doing wrong in my regex?


You can use the Matches() method, which returns a collection of all matches.

Also, A-Z is not really a good solution for text (and indeed the 1 in #test1# will not be matched!!!), since it doesn't include any extended character, such as éàèöäü etc. - you may want to look at \w which matches word characters, or \p{L} to match any letter in any language.

Edit: maybe this would suit your needs better:

#([^\s#]+)#


It will match the first item only

use NextMatch() function

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜