1

RegexLibraryから次のパターンを見つけましたが、Match を使用して Re および Im の値を取得する方法がわかりません。私はの新人ですRegex。パターンからデータを取得する正しい方法ですか? 本当なら、サンプル コードが必要です。これは私がそうあるべきだと思うものです:

public static complex Parse(string s)
{
    string pattern = @"([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?[r]?|[-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)?[i]|[-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?[r]?[-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)?[i])";
    Match res = Regex.Match(s, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);        

    // What should i do here? The complex number constructor is complex(double Re, double Im);

    // on error...
    return complex.Zero;
}

前もって感謝します!

4

3 に答える 3

4

正規表現を少し複雑にしすぎていると思います。たとえば、科学的数値のサポートが含まれており、いくつかのエラーがあるようです。

代わりに、この単純な正規表現を試してください。

class Program
{
    static void Main(string[] args)
    {
        // The pattern has been broken down for educational purposes
        string regexPattern =
            // Match any float, negative or positive, group it
            @"([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)" +
            // ... possibly following that with whitespace
            @"\s*" +
            // ... followed by a plus
            @"\+" +
            // and possibly more whitespace:
            @"\s*" +
            // Match any other float, and save it
            @"([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)" +
            // ... followed by 'i'
            @"i";
        Regex regex = new Regex(regexPattern);

        Console.WriteLine("Regex used: " + regex);

        while (true)
        {
            Console.WriteLine("Write a number: ");
            string imgNumber = Console.ReadLine();
            Match match = regex.Match(imgNumber);

            double real = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
            double img = double.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
            Console.WriteLine("RealPart={0};Imaginary part={1}", real, img);
        }                       
    }
}

使用するすべての正規表現を理解しようとすることを忘れないでください。盲目的に使用しないでください。他の言語と同じように理解する必要があります。

于 2010-10-20T23:28:56.577 に答える
1

これは、Visual Basic .NET 4 での私の見解です。

 

   Private Function GenerateComplexNumberFromString(ByVal input As String) As Complex 

 Dim Real As String = “(?<!([E][+-][0-9]+))([-]?\d+\.?\d*([E][+-][0-9]+)" & _
 "?(?!([i0-9.E]))|[-]?\d*\.?\d+([E][+-][0-9]+)?)(?![i0-9.E])”

 Dim Img As String = “(?<!([E][+-][0-9]+))([-]?\d+\.?\d*([E][+-][0-9]+)?" & _ 
 "(?![0-9.E])(?:i)|([-]?\d*\.?\d+)?([E][+-][0-9]+)?\s*(?:i)(?![0-9.E]))” 

 Dim Number As String = “((?<Real>(” & Real & “))|(?<Imag>(” & Img & “)))”
 Dim Re, Im As Double
 Re = 0
 Im = 0

  For Each Match As Match In Regex.Matches(input, Number)

      If Not Match.Groups(“Real”).Value = String.Empty Then
         Re = Double.Parse(Match.Groups(“Real”).Value, CultureInfo.InvariantCulture)
      End If

     If Not Match.Groups(“Imag”).Value = String.Empty Then
           If Match.Groups(“Imag”).Value.ToString.Replace(” “, “”) = “-i” Then
                  Im = Double.Parse(“-1″, CultureInfo.InvariantCulture)
           ElseIf Match.Groups(“Imag”).Value.ToString.Replace(” “, “”) = “i” Then
                  Im = Double.Parse(“1″, CultureInfo.InvariantCulture)
           Else
                  Im = Double.Parse(Match.Groups(“Imag”).Value.ToString.Replace(“i”, “”), CultureInfo.InvariantCulture)
  End If
  End If
 Next

     Dim result As New Complex(Re, Im)
      Return result
     End Function

于 2011-05-19T09:50:16.667 に答える
1

Captureから 2 つのオブジェクトを取得し、それらの値Matchを呼び出す必要がありDouble.Parseます。

static readonly Regexところで、を呼び出すたびにパターンを再解析する必要がないように、オブジェクトを使用する必要があることに注意してくださいParse。これにより、コードの実行が大幅に高速化されます。

于 2010-10-20T22:56:31.883 に答える