small reg exp pattern needed

  • Thread starter Thread starter Lasse Edsvik
  • Start date Start date
L

Lasse Edsvik

Hello

I was wondering why:

Match m = Regex.Match(@"/great ba b4",@"^/(\w[a-z]+)");

doesnt match: great

have i missed something? im trying to match the first word after /

i get: /great

no matter how i try :(

TIA

/Lasse
 
Lasse said:
Hello

I was wondering why:

Match m = Regex.Match(@"/great ba b4",@"^/(\w[a-z]+)");

doesnt match: great

have i missed something? im trying to match the first word after /

i get: /great

no matter how i try :(

TIA

/Lasse

What's happening is that your Match object contains information on 2
captured groups. When you use a grouping construct in a Regex (ie., you
use the parens) to capture 'substring matches' of the Regex, the Match
object contains a collection of groups that are captured. The first
capture group in that collection is *always* the match of the entire
regular expression, which in this case includes the "^/" portion of the
Regex. That's the Value you're seeing. This corresponds to:

m.Groups[0].Value

If you take a look at m.Groups[1].Value, it will contain the match for
your explicit capture group: "(\w[a-z]+)" - "great"!

Read the Framework's "Grouping Constructs" documentation under the
heading "Regular Expression Language Elements" for details.
 
Back
Top