weston
weston

Reputation: 54811

Regex capturing

I am having trouble capturing a value from a string. I only want the number I don't want to capture the T or :. This failing test explains:

[TestMethod]
public void RegExTest()
{
    var rex = new Regex("^T([0-9]+):"); //as far as I understand, the () denote the capture group
    var match = rex.Match("T12:abc");
    Assert.IsTrue(match.Success);
    Assert.IsTrue(match.Groups[0].Success);
    Assert.AreEqual("12", match.Groups[0].Captures[0]); //fails, actual is "T12:"
}

Upvotes: 1

Views: 196

Answers (3)

Anirudha
Anirudha

Reputation: 32827

So you want to match digits between T and :

Here is a simple Regex for that

@"(?<=T)\d+(?=:)"//no need of groups here

About your Regex:

your regex

^T([0-9]+):

should be like this

T(\d+)://^ is not needed and [0-9] can be represented as \d

Here

Group[0] would be T:12//a full match
Group[1] would be 12//1st match within ()i.e.1st ()
Group[2] would be //2nd match within ()i.e 2nd ()

Upvotes: 1

Kash
Kash

Reputation: 9039

Groups collection, which is zero based, denotes capturing groups from index 1.
Groups[0] always indicates the entire match.
Hence you need to do Groups[1] instead of Groups[0] above.

The MatchGroups property returns a GroupCollection object that contains Group objects that represent captured groups in a single match. The first Group object in the collection (at index 0) represents the entire match. Each object that follows represents the results of a single capturing group.

The Group Collection

Upvotes: 1

weston
weston

Reputation: 54811

Got it by naming the group.

[TestMethod]
public void RegExTest()
{
    var rex = new Regex("^T(?<N>[0-9]+):");
    var match = rex.Match("T12:abc");
    Assert.IsTrue(match.Success);
    Assert.IsTrue(match.Groups[0].Success);
    Assert.AreEqual("12", match.Groups["N"].Value);
}

Should have looked harder: How do I access named capturing groups in a .NET Regex?

Upvotes: 0

Related Questions