我正在尝试创建一个表达式,从给定的字符串输入中提取大于 125 的字符串。
var input = "YH300s, H900H, 234, 90.5, +12D, 48E, R180S, 190A, 350A, J380S";
请查看链接以进一步引用我的脚本/数据示例。
这是我当前的表达式尝试 (*):
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/