0

I want to use regex to parse it into groups

string input = @"(1,2)(3,4)";
Regex.Matches(input, @"\((\d,\d)\)");

The results I get is not only 1,2 and 3,4 but also spaces. Can you guys help me ?

EDIT:

I want to get 2 groups 1,2 and 3,4.

7
  • Please post the output you get as well. Commented Mar 24, 2014 at 10:34
  • Not for me..... Are you using a different input than this? Commented Mar 24, 2014 at 10:34
  • Same as Dave here. Copy-pasted your code, results were (1,2) and (3,4). Commented Mar 24, 2014 at 10:35
  • 2
    As it stands, and based on the description you have given, this works correctly. You will need to post more detail about what you expect to see, and which input has caused you the problems. Commented Mar 24, 2014 at 10:36
  • Using regexpal.com to test your example seems to work fine, ignoring spaces wherever I put them. Commented Mar 24, 2014 at 10:37

4 Answers 4

1
string input = @"(1,2)(3,4)";
 MatchCollection inputMatch= Regex.Matches(collegeRecord.ToString(), @"(?<=\().*?(?=\))");

For current string you will get two outputs:

inputMatch[0].Groups[0].Value;
inputMatch[0].Groups[1].Value;

Or

You can also try foreach loop

 foreach (Match match in inputMatch)
{

}

I have not tested this code,

My Working Example:

MatchCollection facilities = Regex.Matches(collegeRecord.ToString(), @"<td width=""38"">(.*?)image_tooltip");
            foreach (Match facility in facilities)
            {
                collegeDetailDH.InsertFacilityDetails(collegeDetailDH._CollegeID, facility.ToString().Replace("<td width=\"38\">", string.Empty).Replace("<span class=\"icon_", string.Empty).Replace("image_tooltip", string.Empty));
            }
Sign up to request clarification or add additional context in comments.

Comments

0

How do you reach them? Try this:

Example:

MatchCollection matchs = Regex.Matches(input, @"\((\d,\d)\)");
foreach (Match m in matchs)
{
    rtb1.Text += "\n\n" + m.Captures[0].Value;
}

Comments

0

Try looking at this pattern:

(\((?:\d,\d)\))+

+ allows that the group is repeating and can occur one or more time.

1 Comment

Show us some code as well, that is how do you get value from regex match.
0

You need to use lookarounds.

string input = @"(1,2)(3,4)";
foreach (Match match in Regex.Matches(input, @"(?<=\().*?(?=\))"))
    Console.WriteLine(match.Value);

If your string may have other content then digits in brackets, and you need only those with digits inside, you can use more specific regex as follows.

string input = @"(1,2)(3,4)";
foreach (Match match in Regex.Matches(input, @"(?<=\()\d,\d(?=\))"))
    Console.WriteLine(match.Value);

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.