1

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:"
}
weston
  • 54,145
  • 21
  • 145
  • 203

3 Answers3

1

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

Kash
  • 8,799
  • 4
  • 29
  • 48
1

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 ()
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Thanks, the `^` is to ensure that the T is at the start of the string. So I do need that don't I? – weston Aug 22 '12 at 15:34
  • no you don't need it... if you use `^` then a string **sddsT55:** would not match since `^` means that you want T at the begging.So a string **T55:** would match with `^` but not **sddsT55:** – Anirudha Aug 22 '12 at 15:38
  • @weston if you are sure that the `string` would be always `T54:` then you should use `^` – Anirudha Aug 22 '12 at 15:43
  • Yeah I'm sure, see my next question makes it clearer http://stackoverflow.com/questions/12077136/regex-extract-optional-group – weston Aug 22 '12 at 16:07
0

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?

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
  • Well, congrats for answering your own question. Could you give a try to my suggestion, anyway? – Andre Calil Aug 22 '12 at 14:59
  • @AndreCalil I did, and it works, but like I said only by accessing `Group[1]`, which actually as Kash points out works without changing the expression. – weston Aug 22 '12 at 15:03