【问题标题】:Split numeric part from alphanumeric string using C#使用 C# 从字母数字字符串中拆分数字部分
【发布时间】:2013-10-10 06:11:19
【问题描述】:

我有字母数字字符串列表。 例如:

1A
2B
7K
10A

我只想获取数字部分然后比较它们,如果小于 10 我不需要将它添加到另一个列表中。 我想知道从字符串中拆分数字部分的正则表达式。 任何帮助。 到目前为止我所做的是:

 if (x == y) // also handles null
            return 0;
        if (x == null)
            return -1;
        if (y == null)
            return +1;

        int ix = 0;
        int iy = 0;
        while (ix < x.Length && iy < y.Length)
        {
            if (Char.IsDigit(x[ix]) && Char.IsDigit(y[iy]))
            {
                // We found numbers, so grab both numbers
                int ix1 = ix++;
                int iy1 = iy++;
                while (ix < x.Length && Char.IsDigit(x[ix]))
                    ix++;
                while (iy < y.Length && Char.IsDigit(y[iy]))
                    iy++;
                string numberFromX = x.Substring(ix1, ix - ix1);
                string numberFromY = y.Substring(iy1, iy - iy1);

                // Pad them with 0's to have the same length
                int maxLength = Math.Max(
                    numberFromX.Length,
                    numberFromY.Length);
                numberFromX = numberFromX.PadLeft(maxLength, '0');
                numberFromY = numberFromY.PadLeft(maxLength, '0');

                int comparison = _CultureInfo
                    .CompareInfo.Compare(numberFromX, numberFromY);
                if (comparison != 0)
                    return comparison;
            }
            else
            {
                int comparison = _CultureInfo
                    .CompareInfo.Compare(x, ix, 1, y, iy, 1);
                if (comparison != 0)
                    return comparison;
                ix++;
                iy++;
            }
        }

但我不想让我的方法如此复杂。 所以我需要一个正则表达式来拆分。

【问题讨论】:

  • 什么是 x 和 y..为什么不将它们解析为 int 然后比较..向我们展示您的完整代码
  • 从问题中很明显 x 和 y 将是列表中的项目,如 1A、2B 等
  • 你可以在stackoverflow中使用下面的地址stackoverflow.com/questions/3720012/…

标签: c# regex split numeric alphanumeric


【解决方案1】:

试试 char 的 IsDigit 方法

var number = int.Parse(new string(someString.Where(char.IsDigit).ToArray()));
if(number<10)
{
   someList.Add(number);
}

使用 AllIsDigit 你可以只取字符串的数字部分,然后将其解析为 int 并比较 :) 无需使用正则表达式

【讨论】:

  • 这是最好的方法,但它不起作用,语法错误,我的字符串就像“7K,1B”。
  • +1 表示更新的代码并且它正在工作,让我正确测试,我也会接受作为答案。非常感谢你。 :)
【解决方案2】:

您可以使用下面的代码拆分输入字符串并获得数字组和字母组的结果。如果一组不存在,则结果将为空字符串。

string input = "10AAA";
Match m = Regex.Match(input, @"(\d*)(\D*)");

string number = m.Groups[1].Value;
string alpha = m.Groups[2].Value;

【讨论】:

    【解决方案3】:

    你可以试试这个:

      string txt="10A";
      string re1="(\\d+)";  // Integer Number 1
    
      Regex r = new Regex(re1);
      Match m = r.Match(txt);
    

    【讨论】:

    • 您可以删除这两个选项而不会造成伤害。你没有字母,所以 IgnoreCase 没用,你没有 . 所以 Singleline 没用。
    【解决方案4】:

    你想这样做吗?

    int num;
    string stringWithNumbers = "10a";
    if (int.TryParse(Regex.Replace(stringWithNumbers, @"[^\d]", ""), out num))
    {
        //The number is stored in the "num" variable, which would be 10 in this case.
        if (num >= 10)
        {
            //Do something
        }
    
    }
    else
    {
        //Nothing numeric i the string
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多