Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
Dears, i want to get KPN string from the following one (NETHERLANDS)KPN
using C# and regex and special using
C#
Regex.Matches


in other word i want to terminate the () and th value between it to keep the value outside in this case terminate (NETHERLANDS) and keep KPN

Thanks

What I have tried:

i tried this but it is doing the opposite things:
C#
string input = "(Vaue 1) test + test(Vaue 2)bbb * aaa(Vaue 3)";
var matches = Regex.Matches(input, @"(?<=\().+?(?=\))")
                    .Cast<match>()
                    .Select(m => m.Value)
                    .ToList();

foreach (var item in matches)
{
    Console.WriteLine(item);
}

Console.Read();</match>
Posted
Updated 27-Apr-16 2:36am
v3
Comments
Karthik_Mahalingam 27-Apr-16 7:50am    
what is your expected output for
string input = "(Vaue 1) test + test(Vaue 2)bbb * aaa(Vaue 3)";
Member 10266297 27-Apr-16 7:52am    
the expected output is: test + test bbb * aaa
everything without ()

Hi, just try the following function,
C#
private string FindMatch()
{
    string input = textBox1.Text.Trim();
    MatchCollection Matches = Regex.Matches(input, @"(\(.+?\))");

    foreach (Match match in Matches)
    {
        input = input.Replace(match.Value, "");
    }

    return input;
}
 
Share this answer
 
Comments
Member 10266297 27-Apr-16 8:42am    
seem good and smart but i cant change the code behind i have to use the below exactly:

string input = "(Vaue 1) test + test(Vaue 2)bbb * aaa(Vaue 3)";
var matches = Regex.Matches(input, @"(?<=\().+?(?=\))")
.Cast<match>()
.Select(m => m.Value)
.ToList();

foreach (var item in matches)
{
Console.WriteLine(item);
}
Richard Deeming 27-Apr-16 9:22am    
If you can't change the code, then why are you asking us how to change the code?!

If you want to change the behaviour, then you need to change the code.
Member 10266297 28-Apr-16 2:11am    
i am asking if there is a way to pick the string outside the ()
Did you try to use Regex.replace instead of Regex.matches ?

Hope this link will help
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
Comments
Member 10266297 27-Apr-16 8:19am    
i don't want to use replace because it is fixed in the code and the regex can be entered from a textbox so i need to use replace
Patrice T 27-Apr-16 8:27am    
You should read again the documentation !
It is not more fixed with replace than with matches

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