[英]C# Regular Expressions
Say if I have an initial string that could contain either an integer or a double, followed by a timescale. 假设我有一个可能包含整数或双精度数的初始字符串,后跟一个时间刻度。 Eg, it could be 5.5hours or 30 mins, etc. The data I will be receiving in this format is notoriously none uniformed so, for example, I could receive data such as 5.5 hours. 例如,可能是5.5小时或30分钟等。众所周知,我将以这种格式接收的数据没有统一,因此,例如,我可以接收5.5小时的数据。 With the added full stop. 随着加句号。
I wanted a way to extract an integer or double from such strings, however I am struggling with the possible inclusion of additional full stops/periods. 我想要一种从这样的字符串中提取整数或双精度的方法,但是我在努力添加其他句号/句点感到很挣扎。 I can easily isolate the numbers and fullstops by replacing the letters with emptyspace. 通过用空格替换字母,我可以轻松地分离数字和句号。
Can anybody please advise. 任何人都可以请指教。
Thanks. 谢谢。
\d+(?:\.\d+)?
should match your criteria: 应该符合您的条件:
\d+ # Match one or more digits
(?: # Try to match the following group:
\. # a dot
\d+ # one or more digits
)? # End of optional group
So, to iterate over all matches in your string: 因此,要遍历字符串中的所有匹配项:
Regex regexObj = new Regex(@"\d+(?:\.\d+)?");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
// matched number: matchResults.Value
matchResults = matchResults.NextMatch();
}
This regex will not match numbers in exponential notation like 1.05E-6
, obviously. 显然,此正则表达式将不匹配指数表示法(如1.05E-6
中的数字。
If you also want to catch the following timescale, then you can use 如果您还想捕获以下时间范围,则可以使用
(\d+(?:\.\d+)?)\s*(\w+)
Now, after a match, matchResults.Groups[1]
will contain the number. 现在,在匹配之后, matchResults.Groups[1]
将包含数字。 matchResults.Groups[2]
will contain the word following the number which you can then check against your list of allowed words. matchResults.Groups[2]
将在数字后面包含单词,然后您可以根据允许的单词列表检查该单词。 This word is mandatory, ie if it's missing, the entire regex will fail - if you don't want that, add a ?
这个词是必填项,即如果缺少该词,则整个正则表达式都会失败-如果您不希望这样做,请添加?
at the end. 在末尾。
Use (named) groups to extract the info you need: 使用(命名的)组提取所需的信息:
(?'val'\d+\.?\d*).*?
or: (?'val'\\d+.?\\d*)\\w+.? 或:(?'val'\\ d +。?\\ d *)\\ w +。? should do the work, and you'll find the results in the named group 'val'. 应该可以完成工作,然后您将在名为“ val”的组中找到结果。
也许是这样的:
@"\b(\d+(?:\.\d+)?)\s+(?:hours|mins|seconds)\b"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.