0

米国形式 ($1.50) の通貨値に一致するように完璧に機能するこの正規表現があります。

Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");

そのような正規表現を構築する方法を理解するためにいくつかの助けが必要です。R$の$を変更するなど、私の国の形式を変更する必要があります。

msdn のトピックを調べていますが、これまでのところ何も機能していません...

4

2 に答える 2

2

あなたの質問には 3 つの部分がありますが、私には、それは主に「釣り方を学ぶこと」に関するものに思えます。

**A. 必要な正規表現 **

コメントに基づいて、これを探しています(デモを参照):

^R\$\d+(?:\.\d{3})*,\d{2}$

B. 正規表現の説明

これは比較的単純な正規表現で、自動生成された説明を読むことができます。いくつかのサイトがこれを行っています。これがその1つです(元のサイトの方がよく表示されます)。

NODE                     EXPLANATION
--------------------------------------------------------------------------------   \w                       word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------   \^                       '^'
--------------------------------------------------------------------------------   \$                       '$'
--------------------------------------------------------------------------------   (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d{1,3}                  digits (0-9) (between 1 and 3 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    (                        group and capture to \2 (0 or more times
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      \,                       ','
--------------------------------------------------------------------------------
      \d{3}                    digits (0-9) (3 times)
--------------------------------------------------------------------------------
    )*                       end of \2 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \2)
--------------------------------------------------------------------------------    |                        OR
--------------------------------------------------------------------------------
    (                        group and capture to \3:
--------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )                        end of \3
--------------------------------------------------------------------------------   )                        end of \1
--------------------------------------------------------------------------------   (                        group and capture to \4 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d{2}                    digits (0-9) (2 times)
--------------------------------------------------------------------------------   )?                       end of \4 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \4)
--------------------------------------------------------------------------------   $                        before an optional \n, and the end of the
                           string

C. そのような正規表現を構築する方法を学ぶにはどうすればよいですか

ここに私が推奨するリソースがあります。

  1. 書籍: Mastering Regular Expressions (第 3 版)、正規表現クックブック

  2. Web サイト: regular-expressions.info、RexEgg、SO に関する FAQ

  3. ツール: RegexBuddy (商用だが優れたデバッガー)、regex101.com、Debuggex.com

于 2014-05-20T05:04:30.313 に答える
1

R次のように、正規表現に a を追加するだけです。

Regex rmoney = new Regex(@"\w\^R\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
于 2014-05-20T03:26:26.213 に答える