Converting Regular expression match

P

Paul Johnston

Hi Just started using c# to do a task I usually use Perl to do and hit
a limit in my knowledge :)
I have a program which reads from a large text file, extracts certain
lines then gets a string from these lines.

Question
If the object I extract is in the form of a number how can I cast it
into a number so I can prerform mathematical operations on it ?


The following code:

foreach (Match theMatch in theMatches1)
{
if (theMatch.Length !=0)
{
Console.WriteLine("The match is
{0}",theMatch.ToString());
Console.WriteLine(theMatch.Groups["prob"]);
Console.WriteLine("Picking out the number {0} ",
theMatch.Groups["prob"]);
}
}

Gives me the following output:

The match is -0.9454 <s> The 0.1170
-0.9454
Picking out the number -0.9454

How can I turn theMatch.Groups["prob"] into say a double

TIA Paul
 
W

Wessel Troost

How can I turn theMatch.Groups["prob"] into say a doubleHere's just two of the many ways:

double dProb = Convert.ToDouble( theMatch.Groups["prob"] );

or:

double dProb = double.Parse( theMatch.Groups["prob"] );

Greetings,
Wessel
 
P

Paul Johnston

How can I turn theMatch.Groups["prob"] into say a double
Here's just two of the many ways:

double dProb = Convert.ToDouble( theMatch.Groups["prob"] );

or:

double dProb = double.Parse( theMatch.Groups["prob"] );

Greetings,
Wessel


The first compiles but gives a run time error:

Unhandled Exception: System.InvalidCastException: Specified cast is
not valid.
at System.Convert.ToDouble(Object value)
at Wordgrams.Main(String[] args)


The second will not compile and gives:

wordgrams4.cs(60,35): error CS1502: The best overloaded method match
for
'double.Parse(string)' has some invalid arguments
wordgrams4.cs(60,49): error CS1503: Argument '1': cannot convert from
'System.Text.RegularExpressions.Group' to 'string'

Coming to a strongly typed language is such a different world :)
 
H

Hans Kesting

Paul said:
How can I turn theMatch.Groups["prob"] into say a double
Here's just two of the many ways:

double dProb = Convert.ToDouble( theMatch.Groups["prob"] );

or:

double dProb = double.Parse( theMatch.Groups["prob"] );

Greetings,
Wessel


The first compiles but gives a run time error:

Unhandled Exception: System.InvalidCastException: Specified cast is
not valid.
at System.Convert.ToDouble(Object value)
at Wordgrams.Main(String[] args)


The second will not compile and gives:

wordgrams4.cs(60,35): error CS1502: The best overloaded method match
for
'double.Parse(string)' has some invalid arguments
wordgrams4.cs(60,49): error CS1503: Argument '1': cannot convert from
'System.Text.RegularExpressions.Group' to 'string'

Coming to a strongly typed language is such a different world :)

theMatch.Groups["prob"] is a "Group". Convert.ToDouble and double.Parse
don't know how to convert that into a double (as you found out). They *do*
know how to convert from string though.

So the solution is easy: add a .Value to get the matched substring:
theMatch.Groups["prob"].Value

Hans Kesting
 
W

Wessel Troost

The first compiles but gives a run time error:Sorry, I was assuming theMatch.Groups["prob"] returned a string.

Replace

theMatch.Groups["prob"]

with

theMatch.Groups["prob"].Value

That should work.

Greetings,
Wessel
 
P

Paul Johnston

The first compiles but gives a run time error:
Sorry, I was assuming theMatch.Groups["prob"] returned a string.

Replace

theMatch.Groups["prob"]

with

theMatch.Groups["prob"].Value

That should work.

Greetings,
Wessel


Converted the group to a string then the string to a double and it
works !
Fun this :)

Thanks for all the help Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top