Kenoyer130
Kenoyer130

Reputation: 7318

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?

Upvotes: 2

Views: 1598

Answers (2)

Lucero
Lucero

Reputation: 60276

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#]+)#

Upvotes: 3

Thorin Oakenshield
Thorin Oakenshield

Reputation: 14692

It will match the first item only

use NextMatch() function

Upvotes: 2

Related Questions