1

これはおそらく通常の「正規表現を求めている別の人を見てください」ですが、私はこれを理解できません。私がする必要があるのは、3 桁以上または 3 桁未満のものに一致する単純な正規表現を見つけることです。ただし、文字も一致する必要があります。

少しだけ説明。電話番号の標準の市外局番ではないものと一致させようとしています。そう > 3 < 文字を含む. 私はこれをビジネス ルールに使用しており、市外局番の正のバージョンと既に一致しています。

一度に 1 つのレコードのみが正規表現を通過するため、区切り記号は必要ありません。

申し訳ありませんが、いくつかの例を次に示します。

337   : does not match
123   : does not match
12    : does match
1     : does match
asd   : does match
as2   : does match
12as45: does match
1234  : does match

反対は非常に簡単で、[0-9]{3} または [\d]{3} にするだけです。

PSそのJavaで

4

3 に答える 3

3

これが「az」を使用したソリューションです(非常に一般的であるように思われるため):

^(?!\d{3})[a-z0-9]{3}$|^[a-z0-9]{1,2}$|^[a-z0-9]{4,}$

...そして、これが真の解決策です。これは、すべて数字である 3 文字を除くすべてに一致します。

^(?!\d{3}).{3}$|^.{1,2}$|^.{4,}$

http://regexr.com?358u9

3 つの選択肢をチェックしているだけなので、一目瞭然です。

于 2013-06-18T07:32:16.823 に答える
1

このようにできます

^(?:(?!(?<!\d)\d{3}(?!\d))[a-zA-Z\d])+$

こちらの Regexrを参照してください。

説明した

^                                # match the start of the string (not needed with the matches() method)
    (?:                          # start of a non capturing group
        (?!(?<!\d)\d{3}(?!\d))   # combined lookarounds, fails if there are 3 digits following with not a digit before and ahead of those 3 digits
        [a-zA-Z\d]               # match one ASCII letter or digit
    )+                           # repeat this at least once
$                                # match the end of the string (not needed with the matches() method)
于 2013-06-18T08:00:45.347 に答える