Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My example is working fine with greedy when I use to capture the whole value of a string and a group(in group[1] ONLY) enclose with a pair of single quote
But when I want to capture the whole value of a string and a group(in group[1] ONLY) enclose with multiple pair of single quote , it only capture the value of string enclose with last pair but not the string between first and last single quotes.

What I have tried:

string val1 = "Content:abc'23'asad";          //--->23
            string val2 = "Content:'Scale['#13212']'ta";

            Match match1 = Regex.Match(val1, @".*'(.*)'.*");
            Match match2 = Regex.Match(val2, @".*'(.*)'.*");
            if (match1.Success)
            {
                string value1 = match1.Value;
                string GroupValue1 = match1.Groups[1].Value;
                Console.WriteLine(value1);
                Console.WriteLine(GroupValue1);

                string value2 = match2.Value;
                string GroupValue2 = match2.Groups[1].Value;
                Console.WriteLine(value2);
                Console.WriteLine(GroupValue2);

                Console.ReadLine();

                // using greedy For val1 i am getting perfect value for-
                // value1--->Content:abc'23'asad
                // GroupValue1--->23

                // BUT using greedy For val2 i am getting the string elcosed by last single quote-
                // value2--->Content:'Scale['#13212']'ta
                // GroupValue2---> ]
                // But i want GroupValue2--->Scale['#13212']
            }
Posted
Updated 8-May-17 6:44am
Comments
Patrice T 7-May-17 16:33pm    
Show example of input strings and selections you want with the RegEx.

1 solution

Remove the .* from the beginning and end of your pattern:
C#
string val2 = "Content:'Scale['#13212']'ta";
Match match2 = Regex.Match(val2, @"'(.*)'");
if (match2.Success)
{
    string value2 = match2.Value;
    string GroupValue2 = match2.Groups[1].Value;
    Console.WriteLine(value2);
    Console.WriteLine(GroupValue2);
}
Output:
'Scale['#13212']'
Scale['#13212']
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900