我有一个句子,在句子的起点和终点之间,它可以包含任何特殊字符或数字或字母,但不包括单词。
为了更清楚地说明我的观点,我在下面用一个例子来说明:
我有一句话像"Today's Market value 0.5 percent"
现在从上面"市场价值"和"百分比"之间的句子中,我一定没有得到任何其他词。
Statements allowed:
1) "Today's Market value* 0.5 percent"
2) "Today's Market value1 0.5 percent"
3) "Today's Market value 1 0.5 percent"
4) "Today's Market value e 0.5 percent"
5) "Today's Market value 0.5 percent"
Statements not allowed:
1) "Today's market value is 0.5 percent"
2) "Today's market value is 0.5 percent"
3) "Today's Market value is 0.5 percent"
我主要感兴趣的是在这里获取市场价值,即"0.5"。
请向我建议一种构建正则表达式的正确方法,以满足我的上述要求。
如果字符串正常,下面是提取感兴趣数量的代码:
string[] strList = new[] {
@"Today's Market value* 0.5 percent",
@"Today's Market value1 0.5 percent",
@"Today's Market value 1 0.5 percent",
@"Today's Market value e 0.5 percent",
@"Today's Market value 0.5 percent",
@"Today's market value is 0.5 percent",
@"Today's market value is 0.5 percent",
@"Today's Market value is 0.5 percent"
};
foreach (string str in strList)
{
Match m = Regex.Match(str, @"(?<=Market value.*s)(?<!Market value.*[a-zA-Z]{2}.*)d+(.d+)?(?=s.*percent)(?!.*[a-zA-Z]{2}.*percent)", RegexOptions.Singleline);
if (m.Success)
Console.WriteLine("{0} : {1}", m.Value, str);
}
输出:
0.5 : Today's Market value* 0.5 percent
0.5 : Today's Market value1 0.5 percent
0.5 : Today's Market value 1 0.5 percent
0.5 : Today's Market value e 0.5 percent
0.5 : Today's Market value 0.5 percent
基本思想:数字前面应该有市值文本、任何内容和空格,但前面不应该有市值 + 2 个或更多连续字母的任意文本。此外,数字后面应跟空格、任何内容和百分比文本,但不应在任何地方后跟 2 个或更多连续字母 + 百分比。
试试这个正则表达式:
bMarket valueb(?!s+iss)[sS]*?(d+(?:.d+)?)s*percentb
(?!s+iss)
是负面的展望,检查Market value
后没有is
。
在线演示