c# - 匹配组中特定单词模式不同值的正则表达式拆分字符串

标签 c# regex

这个问题与我之前提出的一个问题 ( This Question ) 非常相似,但是我需要稍微修改一下。

所以在前面的问题中这个字符串

Berkshire Hathaway Inc (Ticker: BRK; NAICS: 524126, 511130, 335212, 445292, 511110, 442210; Duns: 00-102-4314) Walt Disney Co (Ticker: DIS; NAICS: 713110, 512110, 711211, 515120; Duns: 00-690-4700)

creates 2 matches with these values:

Berkshire Hathaway Inc
Walt Disney Co

Now I want the matches to contain Ticker: XXX or the Company Name with preference going to Ticker: XXX.

So for the example above it would match:

Ticker: BRK
Ticker: DIS

And for this example:

Berkshire Hathaway Inc (NAICS: 524126, 511130, 335212, 445292, 511110, 442210; Duns: 00-102-4314) Walt Disney Co (Ticker: DIS; NAICS: 713110, 512110, 711211, 515120; Duns: 00-690-4700)

The result would be:

Berkshire Hathaway Inc
Ticker: DIS

I guess I just don't understand the regex solution in the previous question well enough to understand how to modify it to fit this pattern.

The regex is written in c#

By the way the previous regex solution was:

(?!\s*$)(.*?)(?:\([^)]*(?:(?:SIC|NAICS):[^)]*)+\)|$)

我想现在应该改成这样:

(?!\s*$)(.*?)(?:\([^)]*(?:(?:SIC|NAICS|Duns):[^)]*)+\)|$)

但如果 Ticker 存在,我如何提取 Ticker: 并选择该值而不是其他值?

最佳答案

我仍在学习正则表达式,所以我不确定您是否可以对组使用条件逻辑。不过,作为替代方案,您可以按如下方式修改您的正则表达式,以便它也可以为代码捕获一个组(如果它存在):

(?!\s*$)(.*?)(?:\((Ticker:[^;]+)?[^)]*(?:(?:SIC|NAICS|Duns):[^)]*)+\)|$)

然后您可以在您的 C# 代码中执行逻辑。我想这样的事情会起作用:

Regex regex = new Regex(@"(?!\s*$)(.*?)(?:\((Ticker:[^;]+)?[^)]*(?:(?:SIC|NAICS|Duns):[^)]*)+\)|$)");
Match match = regex.Match("Berkshire Hathaway Inc (NAICS: 524126, 511130, 335212, 445292, 511110, 442210; Duns: 00-102-4314) Walt Disney Co (Ticker: DIS; NAICS: 713110, 512110, 711211, 515120; Duns: 00-690-4700)");  
while (match.Success) {
    if (match.Groups[2].Success)
    {
        Console.WriteLine(match.Groups[2].Value);
    }
    else
    {
        Console.WriteLine(match.Groups[1].Value);
    }
    match = match.NextMatch();
}

输出:

Berkshire Hathaway Inc 
Ticker: DIS

关于c# - 匹配组中特定单词模式不同值的正则表达式拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10015986/

相关文章:

c# - 价格正则表达式

javascript - 检查函数是否在 Ajax 和 WebMethod 中返回任何数据

c# - 嵌套的 using 语句 - 哪一个不会被处理掉

c# - 如何在安装项目中包含和链接 Sqlite 数据库文件

c# - 折线图 - 更改边框宽度会删除空间

JavaScript 正则表达式 : Only matching the last pattern

java - 使用正则表达式从java中的逗号分隔字符串中提取特定单词

Java - 解析字符串 - String.split() 与 Pattern 和 Matcher

java - 检查一个字符串是否包含空格和多个@符号

python - 正则表达式在 C++ 中不匹配?