0

I've been trying to capture a string that is between two commas. I created the following code:

Regex.Match(forReg, @"\,([^,]*)\,");

the forReg string will look like this

forReg = "123456,x,NULL"

Where x is an integer less than 999.

The first problem is I'm not sure how to use the string that I've captuered using Regex.Match and the second problem is I'm not even sure if I've done the Regex code correctly. I've looked up several threads with similar issues but can't seem to make any more progress.

2
  • 1
    The regex looks right by me. This has some examples that might help you: dotnetperls.com/regex-match Commented May 26, 2014 at 2:41
  • You don’t need to escape commas though, so ",([^,]*)," would be just fine. Commented May 26, 2014 at 2:57

2 Answers 2

1

Okay so this worked

            Match match = Regex.Match(forReg, @"\,([^,]*)\,");
            if (match.Success)
            {
                string age = match.Groups[1].Value;
            }
Sign up to request clarification or add additional context in comments.

Comments

1

You can access the captured match by using the Match.Groups property, and secondly you do not need to escape the comma's inside your regular expression because it is not a character of special meaning.

String forReg = "123456,77,NULL";
Match match   = Regex.Match(forReg, @",([^,]*),");

if (match.Success) {
   Console.WriteLine(match.Groups[1].Value); //=> "77"
} 

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.