How should i get complex numbers from string?
I found following pattern from RegexLibrary, and i don't know how use Match to get Re and Im values. I'm new in Regex
. Is it a correct way to get data from a p开发者_运维技巧attern?
If it's true, I need some sample code!
This is what i think it should be:
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;
}
Thanks in advance!
I think they're overcomplicating the regex a bit, they are for example including support for scientific numbers and it seems that there are some errors in it.
Try this simpler regex instead.
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);
}
}
}
Remember to try to understand every regex you use, never use them blindly. They need to be understood like any other language.
You need to get the 2 Capture
objects from the Match
and call Double.Parse
on their values.
Note, by the way, that you should use a static readonly Regex
object so that it won't need to re-parse the pattern every time you call Parse
. This will make your code run much faster.
This was my take in 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
精彩评论