我有一个正则表达式:
var topPayMatch = Regex.Match(result, @"(?<=Top Pay)(\D*)(\d+(?:\.\d+)?)", RegexOptions.IgnoreCase);
我必须把它转换成我所做的 int
topPayMatch = Convert.ToInt32(topPayMatchString.Groups[2].Value);
那么现在...
Top Pay: 1,000,000 然后它当前抓取第一个数字,即 1。我想要所有 1000000。 如果最高工资:888,888,那么我想要所有的 888888。 我应该在正则表达式中添加什么?
答案 0 :(得分:0)
您可以使用像 @"(?<=Top Pay: )([0-9,]+)"
这样简单的东西。请注意,此正则表达式将忽略小数。
这将匹配 Top Pay:
之后的所有数字及其逗号,之后您可以将其解析为整数。
示例:
Regex rgx = new Regex(@"(?<=Top Pay: )([0-9,]+)");
string str = "Top Pay: 1,000,000";
Match match = rgx.Match(str);
if (match.Success)
{
string val = match.Value;
int num = int.Parse(val, System.Globalization.NumberStyles.AllowThousands);
Console.WriteLine(num);
}
Console.WriteLine("Ended");
来源:
答案 1 :(得分:0)
如果您使用后视,则不需要捕获组,您可以将 #define VAL1 ((1U << 1) | (1U << 5))
移动到后视中。
要获取值,您可以匹配 1+ 位数字,然后是 \D*
和 1+ 位数字的可选重复。
注意您的示例数据包含逗号且没有点,使用 ,
作为量词表示 0 或 1 次。
?
模式匹配:
(?<=Top Pay\D*)\d+(?:,\d+)*
正向后视,断言左边是 (?<=Top Pay\D*)
和可选的非数字Top Pay
匹配 1 个以上的数字\d+
可选择重复 (?:,\d+)*
和 1+ 位数字看到一个 .NET regex demo 和一个 C# demo
,
输出
string pattern = @"(?<=Top Pay\D*)\d+(?:,\d+)*";
string input = @"Top Pay: 1,000,000
Top Pay: 888,888";
RegexOptions options = RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
var topPayMatch = int.Parse(m.Value, System.Globalization.NumberStyles.AllowThousands);
Console.WriteLine(topPayMatch);
}