简体   繁体   English

使用 RegEx 从字符串中提取特定部分

[英]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 beC#这可能是

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 .在 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. (?<=\\() : 正面查找 for (意味着匹配将在 a (之后开始,而不将其捕获到结果中。

\\d+ : captures all digits in a row until non digit character found \\d+ :捕获一行中的所有数字,直到找到非数字字符

(?=\\)$) : 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. (?=\\)$) :积极展望)与行结束意味着匹配将在 a )之前结束,行结束而不捕获)和行结束到结果。

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.第一个使用最简单的模式,此外还使用 Trim 方法。

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.第三个使用 Lookbehind 和 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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