[英]Regex needs to skip words in named pattern C#
我有一个正循环的正则表达式模式列表,如果成功,请执行一个操作。 这是我现在所拥有的:
string csvText =
"A Minimum Usage Fee of ten dollars and ninety-nine cents ($10.99) for each Customer ESI ID with electricity consumption less than 1,000 kWh per billing cycle.";
string csvPattern =
@"A Minimum Usage Fee of (\p{Sc}*(?<cost>\s?\d+[., ]?\d*)) for each Customer ESI ID with electricity consumption (?<comparator>[a-zA-Z0-9\s]+) (?<usage>[0-9, ]+) kWh per billing cycle.";
Regex regex = new Regex(csvPattern);
var match = regex.Match(csvText);
但是,我有一个会有所不同的字符串(“十美元和九十九美分”),然后是我想要的值(“ 10.99”)。 如何告诉一个命名的正则表达式字符串跳过这些单词? 我尝试过(未成功)在成本组中添加了一个后向功能,但是不匹配。
如果费用格式一致,则不需要(\\p{Sc}*(?<cost>\\s?\\d+[., ]?\\d*))
。
您应该使用(?<cost>\\$\\d+(?:\\.\\d+)?)
string csvText = "A Minimum Usage Fee of ten dollars and ninety-nine cents ($10.99) for each Customer ESI ID with electricity consumption less than 1,000 kWh per billing cycle.";
string csvPattern =
@"A Minimum Usage Fee of [A-Za-z -]+ \((?<cost>\$\d+(?:\.\d+)?)\) for each Customer ESI ID with electricity consumption (?<comparator>[a-zA-Z0-9\s]+) (?<usage>[0-9, ]+) kWh per billing cycle\.";
Regex regex = new Regex(csvPattern);
var match = regex.Match(csvText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.