0

I have string like

"Augustin Ralf (050288)"
"45 Max Müller (4563)"
"Hans (Adam) Meider (056754)"

I am searching for a regex to extract the last part in the brackets, for example this results for the strings above:

"050288"
"4563"
"056754"

I have tried with

 var match = Regex.Match(string, @".*(\(\d*\))");

But I get also the brackets with the result. Is there a way to extract the strings and get it without the brackets?

2
  • please use regex - (([^)]*))[^(]*$. This is working as expected. I have tested here Commented Jul 10, 2021 at 14:24
  • Remember to accept an answer that helped you (and upvote the others if you want). Commented Jul 11, 2021 at 9:32

5 Answers 5

2

Taking your requirements precisely, you are looking for

\(([^()]+)\)$

This will capture anything between the parentheses (not nested!), may it be digits or anything else and anchors them to the end of the string. If you happen to have whitespace at the end, use

\(([^()]+)\)\s*$

In C# this could be

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"\(([^()]+)\)$";
        string input = @"Augustin Ralf (050288)
45 Max Müller (4563)
Hans (Adam) Meider (056754)
";
        RegexOptions options = RegexOptions.Multiline;
        
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

See a demo on regex101.com.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you. But do I have to remove the brackets manually from the matched value?
No, use the first capturing group.
I have to use the second capturing group. The first is the match itself.
@BennoDual: Ok. I'm not much into C# to be honest. Glad, it worked for you though.
2

please use regex - \(([^)]*)\)[^(]*$. This is working as expected. I have tested here

Comments

2

You can extract the number between the parantheses without worring about extracting the capturing groups with following regex.

(?<=\()\d+(?=\)$)

demo

Explanation:

(?<=\() : positive look behind for ( meaning that match will start after a ( without capturing it to the result.

\d+ : captures all digits in a row until non digit character found

(?=\)$) : positive look ahead for ) with line end meaning that match will end before a ) with line ending without capturing ) and line ending to the result.

Edit: If the number can be within parantheses that is not at the end of the line, remove $ from the regex to fix the match.

1 Comment

While this is true, lookarounds are "expensive" and will take significantly longer when the string is longer.
1
 var match = Regex.Match(string, @".*\((\d*)\)");

https://regex101.com/r/Wk9asY/1

Comments

1

Here are three options for you.

The first one uses the simplest pattern and in addition the Trim method.

The second one uses capturing the desired value to the group and then getting it from the group.

The third one uses Lookbehind and Lookahead.

var inputs = new string[] {
    "Augustin Ralf (050288)", "45 Max Müller (4563)", "Hans (Adam) Meider (056754)"
};


foreach (var input in inputs)
{
    var match = Regex.Match(input, @"\(\d+\)");
    Console.WriteLine(match.Value.Trim('(', ')'));
}
Console.WriteLine();


foreach (var input in inputs)
{
    var match = Regex.Match(input, @"\((\d+)\)");
    Console.WriteLine(match.Groups[1]);
}
Console.WriteLine();


foreach (var input in inputs)
{
    var match = Regex.Match(input, @"(?<=\()\d+(?=\))");
    Console.WriteLine(match.Value);
}
Console.WriteLine();

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.