Converting Regular expression match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Johnston

    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.Lengt h !=0)
    {
    Console.WriteLi ne("The match is
    {0}",theMatch.T oString());
    Console.WriteLi ne(theMatch.Gro ups["prob"]);
    Console.WriteLi ne("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

  • Wessel Troost

    #2
    Re: Converting Regular expression match

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

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

    or:

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

    Greetings,
    Wessel

    Comment

    • Paul Johnston

      #3
      Re: Converting Regular expression match

      On Fri, 29 Jul 2005 10:31:45 +0200, "Wessel Troost"
      <nothing@like.t he.sun> wrote:
      [color=blue][color=green]
      >> How can I turn theMatch.Groups["prob"] into say a double
      >>[/color]
      >Here's just two of the many ways:
      >
      > double dProb = Convert.ToDoubl e( theMatch.Groups["prob"] );
      >
      >or:
      >
      > double dProb = double.Parse( theMatch.Groups["prob"] );
      >
      >Greetings,
      >Wessel[/color]


      The first compiles but gives a run time error:

      Unhandled Exception: System.InvalidC astException: 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(6 0,35): error CS1502: The best overloaded method match
      for
      'double.Parse(s tring)' has some invalid arguments
      wordgrams4.cs(6 0,49): error CS1503: Argument '1': cannot convert from
      'System.Text.Re gularExpression s.Group' to 'string'

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

      Comment

      • Hans Kesting

        #4
        Re: Converting Regular expression match

        Paul Johnston wrote:[color=blue]
        > On Fri, 29 Jul 2005 10:31:45 +0200, "Wessel Troost"
        > <nothing@like.t he.sun> wrote:
        >[color=green][color=darkred]
        >>> How can I turn theMatch.Groups["prob"] into say a double
        >>>[/color]
        >> Here's just two of the many ways:
        >>
        >> double dProb = Convert.ToDoubl e( theMatch.Groups["prob"] );
        >>
        >> or:
        >>
        >> double dProb = double.Parse( theMatch.Groups["prob"] );
        >>
        >> Greetings,
        >> Wessel[/color]
        >
        >
        > The first compiles but gives a run time error:
        >
        > Unhandled Exception: System.InvalidC astException: 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(6 0,35): error CS1502: The best overloaded method match
        > for
        > 'double.Parse(s tring)' has some invalid arguments
        > wordgrams4.cs(6 0,49): error CS1503: Argument '1': cannot convert from
        > 'System.Text.Re gularExpression s.Group' to 'string'
        >
        > Coming to a strongly typed language is such a different world :-)[/color]

        theMatch.Groups["prob"] is a "Group". Convert.ToDoubl e 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


        Comment

        • Wessel Troost

          #5
          Re: Converting Regular expression match

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

          Replace

          theMatch.Groups["prob"]

          with

          theMatch.Groups["prob"].Value

          That should work.

          Greetings,
          Wessel

          Comment

          • Paul Johnston

            #6
            Re: Converting Regular expression match

            On Fri, 29 Jul 2005 11:15:18 +0200, "Wessel Troost"
            <nothing@like.t he.sun> wrote:
            [color=blue][color=green]
            >> The first compiles but gives a run time error:
            >>[/color]
            >Sorry, I was assuming theMatch.Groups["prob"] returned a string.
            >
            >Replace
            >
            > theMatch.Groups["prob"]
            >
            >with
            >
            > theMatch.Groups["prob"].Value
            >
            >That should work.
            >
            >Greetings,
            >Wessel[/color]


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

            Thanks for all the help Paul

            Comment

            Working...