Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下字符串要处理
Name ( $4 USD ) Name ( $4.1 USD ) Name ( $4.12 USD ) Name ( $4123123.02 USD )
我想从他们那里得到美元现在我正在使用以下正则表达式模式
Regex regex = new Regex(@"\( \$(?<cash>\d+\.\d{1,2}) USD \)");
但它与第一个示例不匹配。我应该更改什么以使 .\d{1,2} 部分可选?谢谢!
Regex regex = new Regex(@"\( \$(?<cash>\d+(?:\.\d{1,2})?) USD \)");
(?:...)是一个非捕获组,以下?使该组可选。
(?:...)
?