简体   繁体   English

正则表达式需要跳过命名模式C#中的单词

[英]Regex needs to skip words in named pattern C#

I have a list of regex patterns I'm looping through, and doing an action if it is successful. 我有一个正循环的正则表达式模式列表,如果成功,请执行一个操作。 Here's what I have now: 这是我现在所拥有的:

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);

However, I have a string that will vary ("ten dollars and ninety-nine cents"), and then the value I want ("10.99"). 但是,我有一个会有所不同的字符串(“十美元和九十九美分”),然后是我想要的值(“ 10.99”)。 How do I tell a named regex string to skip those words? 如何告诉一个命名的正则表达式字符串跳过这些单词? I've tried (unsuccessfully) adding a look-behind in the cost group, but it does not match. 我尝试过(未成功)在成本组中添加了一个后向功能,但是不匹配。

If the cost format is consistent then you don't need (\\p{Sc}*(?<cost>\\s?\\d+[., ]?\\d*)) . 如果费用格式一致,则不需要(\\p{Sc}*(?<cost>\\s?\\d+[., ]?\\d*))

You should go with (?<cost>\\$\\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);

Regex101 Demo Regex101演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM