简体   繁体   English

C# 区分第一匹配和第二匹配

[英]C# Distinguish between first and second match

I am using regex to extract the numbers from a string which contains a range.我正在使用正则表达式从包含范围的字符串中提取数字。 The range could be "less than x" , "greater than x" or "between x and y" :范围可以是"less than x""greater than x""between x and y"

"10 - 22"
"< 0,5"
"3,50000 - 11,0"
"< 120000"  
"> 12"

Below is the relevant code snippet.下面是相关的代码片段。 In the case of "less than x" and "greater than x" I use the RegEx (\d*,\d*)?(\d*) to capture the integer/decimal."less than x""greater than x"的情况下,我使用 RegEx (\d*,\d*)?(\d*)来捕获整数/小数。

Low = r.Descr.Contains('>') 
    ? new Quantity {
        Value = Convert.ToDecimal(Regex.Match(r.Descr, @"(\d*,\d*)?(\d*)").Value)
    } 
    : r.Descr.Contains('-') 
    ? new Quantity {
        Value = Convert.ToDecimal(Regex.Match(r.Descr, @"").Value) 
    } 
    : null,
High = r.Descr.Contains('<') 
    ? new Quantity {
        Value = Convert.ToDecimal(Regex.Match(r.Descr, @"(\d*,\d*)?(\d*)").Value) 
    }
    : r.Descr.Contains('-') 
    ? new Quantity { 
        Value = Convert.ToDecimal(Regex.Match(r.Descr, @"").Value) 
    } 
    : null,

In the case of "between x and y" I am having difficulties in constructing a RegEx which would extract the relevant number."between x and y"的情况下,我在构建一个提取相关数字的 RegEx 时遇到了困难。 Is there a way to do this using RegEx?有没有办法使用正则表达式来做到这一点?

try this expression:试试这个表达式:

(\d+,?\d+\s*-\s*\d+,?\d+)|(<\s*\d+,?\d+)|(>\s*\d+,?\d+)

You can use您可以使用

var match = Regex.Match(text, @"(\d+(?:,\d+)?)\s*-\s*(\d+(?:,\d+)*)");

See the regex demo .请参阅正则表达式演示 Details :详情

  • (\d+(?:,\d+)?) - Capturing group 1: one or more digits followed with an optional occurrence of a comma and one or more digits (\d+(?:,\d+)?) - 捕获组 1:一位或多位数字后跟可选的逗号和一位或多位数字
  • \s*-\s* - a - enclosed with zero or more whitespace chars \s*-\s* - a -用零个或多个空格字符括起来
  • (\d+(?:,\d+)*) - Capturing group 2: one or more digits followed with an optional occurrence of a comma and one or more digits (\d+(?:,\d+)*) - 捕获组 2:一位或多位数字后跟可选的逗号和一位或多位数字

Now, match contains a 3,50000 - 11,0 substring in Group 0, and the rest two groups contain your values:现在, match在第 0 组中包含3,50000 - 11,0 11,0 substring,并且 rest 两组包含您的值:

if (match.Success)
{
    Console.WriteLine("{0} - first number", match.Groups[1].Value);
    Console.WriteLine("{0} - second number", match.Groups[2].Value);
}

See the C# demo :请参阅C# 演示

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
public class Test
{
    public static void Main()
    {
        var text = "some words....3,50000 - 11,0 .... text....";
        var match = Regex.Match(text, @"(\d+(?:,\d+)?)\s*-\s*(\d+(?:,\d+)*)");
 
        if (match.Success)
        {
            Console.WriteLine("{0} - first number", match.Groups[1].Value);
            Console.WriteLine("{0} - second number", match.Groups[2].Value);
        }
    }
}

Output: Output:

3,50000 - first number
11,0 - second number

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

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