正则表达式仅捕获数字字段,剥离$和逗号,如果有任何字母数字,则无匹配

发布于 2025-01-23 22:42:25 字数 420 浏览 3 评论 0 原文

我正在尝试写一条正则票据,该正则可以从一个值中剥离出$,并且如果有其他非数字,则根本不匹配。

$100 -> 100
$12,203.00 -> 12203.00
12JAN2022 -> no match

我已经接近了这一点:

^(?:[$,]*)(([0-9.]{1,3})(?:[,.]?))+(?:[$,]*)$

但是,这并不能以$ 1的价格正确捕获数字值,因为重复数字被捕获为像子组捕获一样,您可以在此处看到 https://regex101.com/r/4bojtb/1

I'm trying to write a regex that will strip out $ and , from a value and not match at all if there are any other non-numerics.

$100 -> 100
$12,203.00 -> 12203.00
12JAN2022 -> no match

I have gotten sort of close with this:

^(?:[$,]*)(([0-9.]{1,3})(?:[,.]?))+(?:[$,]*)$

However this doesn't properly capture the numeric value with $1 as the repeating digits are captured as like subgroup captures as you can see here https://regex101.com/r/4bOJtB/1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2025-01-30 22:42:25

您可以使用命名的捕获组捕获数字的所有部分,然后将它们连接。虽然,替换不需要作为后处理步骤的所有炭是更直接的。

这是一个示例代码:

var pattern = @"^\$*(?:(?<v>\d{1,3})(?:,(?<v>\d{3}))*|(?<v>\d+))(?<v>\.\d+)?$";
var tests = new[] {"$100", "$12,203.00", "12JAN2022"};
foreach (var test in tests) {
    var result = string.Concat(Regex.Match(test, pattern)?
            .Groups["v"].Captures.Cast<Capture>().Select(x => x.Value));
    Console.WriteLine("{0} -> {1}", test, result.Length > 0 ? result : "No match");
}

请参阅 c#demo 。输出:

$100 -> 100
$12,203.00 -> 12203.00
12JAN2022 -> No match

正则是

^\$*(?:(?<v>\d{1,3})(?:,(?<v>\d{3}))*|(?<v>\d+))(?<v>\.\d+)?$

请参见详细信息:

  • ^ - 字符串的开始
  • \ $* - 零或更多美元符号
  • (?:(? d {1,3})(?:,(?&lt; v&gt; \ d {3}))*|(? “ v”),然后零或更多逗号的逗号,然后出现三个数字(被捕获为“ V”组),或一个或多个数字(被捕获为“ V”组为“ V”)
  • (?&lt; v&gt; \。 \ d+)? - 可选出现和一个或多个数字(全部捕获到组“ V”)
  • $ - 字符串的结尾。

You can use a named capturing group to capture all parts of the number and then concatenate them. Although, it is more straight-forward to replace all chars you do not need as a post-processing step.

Here is an example code:

var pattern = @"^\$*(?:(?<v>\d{1,3})(?:,(?<v>\d{3}))*|(?<v>\d+))(?<v>\.\d+)?
quot;;
var tests = new[] {"$100", "$12,203.00", "12JAN2022"};
foreach (var test in tests) {
    var result = string.Concat(Regex.Match(test, pattern)?
            .Groups["v"].Captures.Cast<Capture>().Select(x => x.Value));
    Console.WriteLine("{0} -> {1}", test, result.Length > 0 ? result : "No match");
}

See the C# demo. Output:

$100 -> 100
$12,203.00 -> 12203.00
12JAN2022 -> No match

The regex is

^\$*(?:(?<v>\d{1,3})(?:,(?<v>\d{3}))*|(?<v>\d+))(?<v>\.\d+)?$

See the regex demo. Details:

  • ^ - start of string
  • \$* - zero or more dollar symbols
  • (?:(?<v>\d{1,3})(?:,(?<v>\d{3}))*|(?<v>\d+)) - either one to three digits (captured into Group "v") and then zero or more occurrences of a comma and then three digits (captured into Group "v"), or one or more digits (captured into Group "v")
  • (?<v>\.\d+)? - an optional occurrence of . and one or more digits (all captured into Group "v")
  • $ - end of string.
孤君无依 2025-01-30 22:42:25

我不知道如何在单一言论中实现这一目标,但是在这里个人意见我发现将问题分为较小的步骤是一个好主意 - 将来更容易实施和维护/理解而不牺牲时间了解魔术。

  1. 替换所有 $ 到空字符串
    [\ $ \,] =&gt; ``

  2. 仅匹配数字和时期作为捕获组(当然,您可能需要将其与允许的期间位置等相一致)
    ^((\ d {1,3} \。?)+)$

希望这会有所帮助!

I don't know how to achieve this in single regexp, but personal opinion here I find dividing the problem into smaller steps a good idea - it's easier to implement and maintain/understand in the future without sacrificing time to understand the magic.

  1. replace all $ and , to empty string
    [\$\,] => ``

  2. match only digits and periods as a capture group (of course you may need to align this with your requirements on allowed period locations etc.)
    ^((\d{1,3}\.?)+)$

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文