small reg exp pattern needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lasse Edsvik

    #1

    small reg exp pattern needed

    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








  • mikeb

    #2
    Re: small reg exp pattern needed

    Lasse Edsvik wrote:[color=blue]
    > 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
    >[/color]

    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.

    --
    mikeb

    Comment

    Working...