1

I have seen that there are some posts regarding the Java Matcher class, but I was not able to find one regarding the specific methods find() and group().

I have this piece of code, where Lane and IllegalLaneException have already been defined:

private int getIdFromLane(Lane lane) throws IllegalLaneException {
    Matcher m = pattern.matcher(lane.getID());
    if (m.find()) {
        return Integer.parseInt(m.group());
    } else {
        throw new IllegalLaneException();
    }
}

Looking at the Java Documentation, we have the following:

find() - Attempts to find the next subsequence of the input sequence that matches the pattern.

group() - Returns the input subsequence matched by the previous match.

My question is, which is the equivalent to the methods find() and group() in C#?

EDIT: I forgot to say that I am using the MatchCollection class together with Regex

C# code:

private static Regex pattern = new Regex("\\d+$"); // variable outside the method

private int getIdFromLane(Lane lane) //throws IllegalLaneException
{
    MatchCollection m = pattern.Matches(lane.getID());
...
}

2 Answers 2

1

On C# you will use Regex. Regex class has a function named "Matches" which will return all coincident matches for the pattern.

And each Match has a property called Groups where are stored captured groups.

So, find -> Regex.Matches, group -> Match.Groups.

They're not direct equivalents, but they will give you the same functionality.

Here is a simple example:

var reg = new Regex("(\\d+)$");

var matches = reg.Matches("some string 0123");

List<string> found = new List<string>();

if(matches != null)
{
    foreach(Match m in matches)
        found.Add(m.Groups[1].Value);
}

//do whatever you want with found

Remember that m.Groups[0] will contain the full capture and any subsequent Group will be captured groups.

Also, if you expect just one result then you can use .Match:

var match = reg.Match("some string 0123");

if(match != null && match.Success)
    //Process the Groups as you wish.
Sign up to request clarification or add additional context in comments.

3 Comments

Mmm, Ok, I think I missunderstood the meaning of Regex.Matches at first! But then, if Regex.Matches is null, how could I handle it?
That usually means there's no match at all. Let me post an example.
Also, if you expect just one result you can use .Match, it will return just a match and you can chec .Success property of it to check if it was matched.
1

More complete example from @Gusman, I only made it easier cut/paste/understanding and including the required using statement. (I cannot edit, queue is full so doing this for future users...)

using System.Text.RegularExpressions;

public static void SampleRegexMatchesAndMatch()
{
  var reg = new Regex("(\\d+)$");

  //Example with Matches
  var matches = reg.Matches("some string 0123");

  List<string> found = new List<string>();

  foreach(Match m in matches)
    found.Add(m.Groups[1].Value);

  //Do whatever you want with found object results or work direct with matches
  //--------------------------------------------------------------------------

  //Example with Match
  var match = reg.Match("some string 0123");

  if(match != null && match.Success)
  {
    //Process the Groups as you wish.
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.