繁体   English   中英

特定数字和特殊字符的正则表达式

[英]Regex for specific numbers and special characters

我想从下面的输入字符串中提取值 9.6%,2021,4th 并尝试使用不同的解决方案,但没有按预期显示 output。 输入字符串: Gross Domestic Product decreased by 5.9% in volume in the fourth quarter 2021 and by 9.6% in 2021 - 4th Quarter 2021

var pattren = "^-?\\d*(\\.\\d+)?$";
var result  = Regex.Match(s, pattren); 

我提出的解决方案可能不是最佳解决方案,因为我认为它需要额外的步骤。

  1. 通过(\d+\.*\d*%)\s*in\s*(\d{4})\s*-\s*(1st|2nd|3rd|4th)得到想要的部分

  2. 由于您的问题存在细微差别,因此下一步有两种可能性:

  • Replace() in and - to ,得到字符串"9.6%,2021,4th"
  • MatchGroup 1、2、3 组将是"9.6%""2021""4th"

问题是,处理文本总是有惊喜等着你。 测试更多情况并调整方法以处理异常输入。

以供参考:

暂无
暂无

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

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