0

全て、

ユーザーが端数を入力したかどうかを検出するために、.NETで機能する2つの正規表現が必要です。

  1. 整数を含まない小数値のみ(1 1 / 4、3 1 / 2などをチェックしたくない)のみ:1 / 2、3 / 4、8 / 3など。分子、分母は次のようになります。浮動小数点または整数。

  2. 1 / 3、2 / 3、11/4などのすべての有効な分数。

ありがとう。

4

2 に答える 2

1

これを試して:

/// <summary>
/// A regular expression to match fractional expression such as '1 2/3'.
/// It's up to the user to validate that the expression makes sense. In this context, the fractional portion
/// must be less than 1 (e.g., '2 3/2' does not make sense), and the denominator must be non-zero.
/// </summary>
static Regex FractionalNumberPattern = new Regex(@"
    ^                     # anchor the start of the match at the beginning of the string, then...
    (?<integer>-?\d+)     # match the integer portion of the expression, an optionally signed integer, then...
    \s+                   # match one or more whitespace characters, then...
    (?<numerator>\d+)     # match the numerator, an unsigned decimal integer
                          #   consisting of one or more decimal digits), then...
    /                     # match the solidus (fraction separator, vinculum) that separates numerator from denominator
    (?<denominator>\d+)   # match the denominator, an unsigned decimal integer
                          #   consisting of one or more decimal digits), then...
    $                     # anchor the end of the match at the end of the string
    ", RegexOptions.IgnorePatternWhitespace
    );

/// <summary>
/// A regular expression to match a fraction (rational number) in its usual format.
/// The user is responsible for checking that the fraction makes sense in the context
/// (e.g., 12/0 is perfectly legal, but has an undefined value)
/// </summary>
static Regex RationalNumberPattern = new Regex(@"
    ^                     # anchor the start of the match at the beginning of the string, then...
    (?<numerator>-?\d+)   # match the numerator, an optionally signed decimal integer
                          #   consisting of an optional minus sign, followed by one or more decimal digits), then...
    /                     # match the solidus (fraction separator, vinculum) that separates numerator from denominator
    (?<denominator>-?\d+) # match the denominator, an optionally signed decimal integer
                          #   consisting of an optional minus sign, followed by one or more decimal digits), then...
    $                     # anchor the end of the match at the end of the string
    " , RegexOptions.IgnorePatternWhitespace );
于 2012-07-30T17:42:09.067 に答える
0

はじめて

分子または分母が任意の長さである可能性がある、の形式の任意の分数については##/##、次を使用できます。

\d+(\.\d+)?/\d+(\.\d+)?

少なくとも1つ以上の数字がある限り、文字通りのスラッシュの直前と直後にできるだけ多くの数字を取得します。小数点がある場合は、その後に1桁以上の数字を続ける必要がありますが、そのグループ全体はオプションであり、1回しか表示できません。

第二に

分数でなければならないので、整数だけで1は数えられないので、次のように前面に貼り付けてください。

\d*\s*

分数の残りの部分の前に、いくつかの数字と空白を取得します。

于 2012-07-30T15:56:36.840 に答える