简体   繁体   中英

Use RegEx to extract specific part from string

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?

请使用正则表达式 - \\(([^)]*)\\)[^(]*$ 。这是按预期工作的。我在这里测试

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 .

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.

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

https://regex101.com/r/Wk9asY/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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM