简体   繁体   English

正则表达式 - 匹配某个字符串并获取字符串的整数值

[英]Regex - Match a certain string and get the integer value of the string

I have a regex:我有一个正则表达式:

var example= Regex.Match(result, @"\b(Today is:)[\s:]*(.*)", RegexOptions.IgnoreCase);

and then I convert然后我转换

example= int.Parse(result.Groups[2].Value, System.Globalization.NumberStyles.AllowThousands);

This works fine most of the time, however, I noticed that if I have extra string after Today is: for example,这在大多数情况下都可以正常工作,但是,我注意到如果今天之后我有额外的字符串是:例如,

Today is (extra):

My regex above fails for this case, because it also grabs "(extra)" and then goes to int.Parse, it fails.我上面的正则表达式在这种情况下失败,因为它也抓取“(额外)”然后转到 int.Parse,它失败了。 I want my regex to match when there's "Today is:", then it doesn't matter even if there are more strings, just get the string and convert into int value.我希望我的正则表达式在有“今天是:”时匹配,那么即使有更多字符串也没关系,只需获取字符串并转换为 int 值。

For example, Today is: 100,000,000 -> convert and get int 100000000例如,今天是:100,000,000 -> 转换并得到 int 100000000

Today is (abc123): 88,888 -> convert and get int 88888今天是 (abc123): 88,888 -> 转换并得到 int 88888

Today is (Extra Text blah blah): 100,000 -> convert and get int 100000今天是 (Extra Text blah blah): 100,000 -> 转换并得到 int 100000

I would change a bit your regex like this:我会像这样改变你的正则表达式:

\bToday is\b.*?\s*:\s*([\d,\.]+)

Test it here: https://regex101.com/r/jPb6Pa/1在这里测试: https : //regex101.com/r/jPb6Pa/1

Explanation:解释:

  • \\bToday is\\b for searching " Today is " and not " Blablatoday isn't " or something like that. \\bToday is\\b用于搜索“今天是”而不是“ Bblablatoday 不是”或类似的东西。

  • .*? searches anything after " Today is " but in an ungready way.搜索“今天是”之后的任何内容,但未准备好。

  • \\s*:\\s* searches for the ":" char with or without spaces around. \\s*:\\s*搜索带有或不带有空格的 ":" 字符。

  • The capturing group n°1 ([\\d,\\.]+) will search for digits, points and commas, at least one character.捕获组 n°1 ([\\d,\\.]+)将搜索数字、点和逗号,至少一个字符。 It could be improved as a single comma or point would be wrong.它可以改进,因为单个逗号或点是错误的。 But it does the job for the moment.但它暂时完成了这项工作。

You can use您可以使用

\bToday\s+is\b(?:.*?\([^()]*\))?.*?\b(\d+(?:,\d{3})*(?:\.\d+)?)

See the regex demo .请参阅正则表达式演示 Parse the match.Groups[1].Value .解析match.Groups[1].Value

Details :详情

  • \\bToday\\s+is\\b - Today is as whole words with any one or more whitespaces in between \\bToday\\s+is\\b - Today is一个完整的单词,中间有一个或多个空格
  • (?:.*?\\([^()]*\\))? - an optional sequence of any zero or more chars other than newline char as few as possible followed with a ( , zero or more chars other than ( and ) and then a ) char - 除换行符以外的任何零个或多个字符的可选序列,后跟一个( ,零个或多个除()以外的字符,然后是 a )字符
  • .*? - any zero or more chars other than newline char as few as possible - 尽可能少的除换行符以外的零个或多个字符
  • \\b - a word boundary \\b - 单词边界
  • (\\d+(?:,\\d{3})*(?:\\.\\d+)?) - Group 1: a number pattern. (\\d+(?:,\\d{3})*(?:\\.\\d+)?) - 第 1 组:数字模式。

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

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