473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Converting Regular expression match

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

Nov 17 '05 #1
5 2822
> 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
Nov 17 '05 #2
On Fri, 29 Jul 2005 10:31:45 +0200, "Wessel Troost"
<no*****@like.the.sun> wrote:
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 :-)

Nov 17 '05 #3
Paul Johnston wrote:
On Fri, 29 Jul 2005 10:31:45 +0200, "Wessel Troost"
<no*****@like.the.sun> wrote:
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
Nov 17 '05 #4
> 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
Nov 17 '05 #5
On Fri, 29 Jul 2005 11:15:18 +0200, "Wessel Troost"
<no*****@like.the.sun> wrote:
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

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.