(c# regex) 如何获取数字

问题描述 投票:0回答:1
myPattern = @"?:[.]?[\d]+(?:[,.]*\d)*";

但是,我想排除美元符号 ($) 后面的数字。 并希望排除括号 (){}[] 中的数字。

例如:

input text (match)
-----------------------------
abc123,000.5xyz (match:123,000.5)
abc.5xyz (match:.5)
abc0.2xyz  (match:0.2)
abc123.2xyz (match:123.2)
abc$123xyz (false)
abc(123)77xyz (match:77)
abc12{123}xyz (match:12)
abc[123]xyz (false)
c# regex
1个回答
0
投票

您可以使用这个正则表达式:

(?<![${([\d])\d+(,\d{3})*(\.\d+)?(?![]})])

它匹配:

  • (?<![${([\d])
    :对
    $
    (
    [
    {
    或数字进行负向后查找(后者阻止从数字序列的中间进行匹配)
  • \d+(,\d{3})*(\.\d+)?
    :正则表达式匹配带有可选千位和小数部分的数字
  • (?![]})])
    :对
    ]
    }
    )
  • 进行否定前瞻

正则表达式演示 regex101

© www.soinside.com 2019 - 2024. All rights reserved.