简体   繁体   English

正则表达式匹配单词后跟文本的十进制

[英]Regex match word followed by decimal from text

I want to be able to match the following examples and return array of matches 我希望能够匹配以下示例并返回匹配数组

given text: 给定文字:

some word
another 50.00 
some-more 10.10 text
another word

Matches should be (word, followed by space then decimal number (Optionally followed by another word): 匹配应该是(单词,后跟空格,然后是十进制数字(可选地后跟另一个单词):

another 50.00 
some-more 10.10 text

I have the following so far: 到目前为止,我有以下内容:

     string pat = @"\r\n[A-Za-z ]+\d+\.\d{1,2}([A-Za-z])?";
        Regex r = new Regex(pat, RegexOptions.IgnoreCase);
        Match m = r.Match(input);

but it only matches first item: another 50.00 但它只匹配第一项: another 50.00

You do not account for - with [A-Za-z ] and only match some text after a newline. 您没有考虑-使用[A-Za-z ]并仅在换行符后匹配某些文字。

You can use the following regex: 您可以使用以下正则表达式:

[\p{L}-]+\p{Zs}*\d*\.?\d{1,2}(?:\p{Zs}*[\p{L}-]+)?

See the regex demo 请参阅正则表达式演示

The [\\p{L}-]+ matches 1 or more letters and hyphens, \\p{Zs}* matches 0 or more horizontal whitespace symbols, \\d*\\.?\\d{1,2} matches a float number with 1 to 2 digits in the decimal part, and (?:\\p{Zs}*[\\p{L}-]+)? [\\p{L}-]+匹配1个或多个字母和连字符, \\p{Zs}*匹配0个或更多水平空白符号, \\d*\\.?\\d{1,2}匹配浮点数小数部分中的1到2位数,和(?:\\p{Zs}*[\\p{L}-]+)? matches an optional word after the number. 匹配数字后的可选单词。

Here is a C# snippet matching all occurrences based on Regex.Matches method : 这是一个基于Regex.Matches方法匹配所有Regex.Matches的C#片段:

var res = Regex.Matches(str, @"[\p{L}-]+\p{Zs}*\d*\.?\d{1,2}(?:\p{Zs}*[\p{L}-]+)?")
              .Cast<Match>()
              .Select(p => p.Value)
              .ToList();

Just FYI: if you need to match whole words, you can also use word boundaries \\b : 仅供参考:如果您需要匹配整个单词,您还可以使用单词边界\\b

\b[\p{L}-]+\p{Zs}*\d*\.?\d{1,2}(?:\p{Zs}*[\p{L}-]+)?\b

And just another note: if you need to match diacritics, too, you may add \\p{M} to the character class containing \\p{L} : 还有另一个注意事项:如果你需要匹配变音符号,你可以将\\p{M}添加到包含\\p{L}的字符类中:

[\p{L}\p{M}-]+\p{Zs}*\d*\.?\d{1,2}(?:\p{Zs}*[\p{L}\p{M}-]+)?\b

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

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