c# - 使用正则表达式提取特定值

标签 c# regex

我正在尝试创建一个表达式,从给定的字符串输入中提取大于 125 的字符串。

var input = "YH300s, H900H, 234, 90.5, +12D, 48E, R180S, 190A, 350A, J380S";

请查看链接以进一步引用我的脚本/数据示例。

DonotFiddle_Regex example

这是我当前的表达式尝试 (*):

Regex.Matches(input,@"(?!.*\..*)[^\s\,]*([2-5][\d]{2,})[^\s\,]*"))

根据上面的表达式,唯一的输出是350A, J380S

但是我想从输入字符串中提取以下输出(请参阅上面的链接以获取更多引用):

YH300s, H900H, R180S, 190A, 350A, J380S

任何关于我可能出错的地方的进一步指导将不胜感激。如果我的上下文不清楚,请提前道歉,因为我在编写正则表达式方面仍然是新手。

最佳答案

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // an example of input
        var input = "YH300s, H900H, 234, 90.5, +12D, 48E, R180S, 190A, 350A, J380S";

        var parts = input.Split(new[]{", "}, StringSplitOptions.RemoveEmptyEntries);
        // regex for numbers (including negative and floating-point)
        var regex = new Regex(@"[-]?[\d]?[\.]?[\d]+");

        foreach(var part in parts)
        {
            // there can be many matches, e.g. "A100B1111" => "100" and "1111"            
            foreach(Match m in regex.Matches(part))
            {
                if (double.Parse(m.Value) > 125)
                {
                    Console.WriteLine(part);
                    break;
                }
            }                   
        }           
    }
}

输出

YH300s
H900H
234
R180S
190A
350A
J380S

关于c# - 使用正则表达式提取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30507489/

相关文章:

python - "Expected string or buffer"使用 Beautiful Soup 时出错

java - 查找子字符串出现的总数

c# - 使用 ASP.NET Framework 的简单 POST

c# - C# 中具有两种不同数据类型的二维数组

c# - 在 C# 中使用相同布局强制不同命名空间中的类型

r - 如何否定lookbehind正则表达式?

php - 如何使用带有 'e' 修饰符的 preg_replace() 传递参数?

c# - Unity 游戏不适合 Android 屏幕

C# 从字节数组中解析非统一位序列

php - Preg_match_all 和 javascript 数组