How can I do this without Regex ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Conner

    How can I do this without Regex ?


    Is there a way to write a faster function ?

    public static bool IsNumber( char Value )
    {
    if (Regex.IsMatch( Value.ToString( ), @"^[0-9]+$" ))
    {
    return true;
    }
    else return false;
    }

    I think, if I have to repat this some thousands time, it
    could become very time consuming.

    Thanks in advance,

    P.S.
    Is there in C# something similar to Delphi's sets ?

  • Jon Skeet [C# MVP]

    #2
    Re: How can I do this without Regex ?

    Tim Conner <TimConner@disc ussions.microso ft.com> wrote:[color=blue]
    > Is there a way to write a faster function ?
    >
    > public static bool IsNumber( char Value )
    > {
    > if (Regex.IsMatch( Value.ToString( ), @"^[0-9]+$" ))
    > {
    > return true;
    > }
    > else return false;
    > }
    >
    > I think, if I have to repat this some thousands time, it
    > could become very time consuming.[/color]

    While regular expressions are very powerful, they're not very fast for
    doing simple thing - and you're creating a new string for each call as
    well. Far simpler and faster is:

    public static bool IsNumber (char value)
    {
    return (value>='0' && value <='9');
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Bsiang Tan

      #3
      Re: How can I do this without Regex ?

      Why don't you try the framework given method ?
      Check the Char.IsNumber( ) ?

      For example :
      Console.WriteLi ne(Char.IsNumbe r('8')); // Output: "True"



      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1a0437 8e27b421af98994 2@msnews.micros oft.com...[color=blue]
      > Tim Conner <TimConner@disc ussions.microso ft.com> wrote:[color=green]
      > > Is there a way to write a faster function ?
      > >
      > > public static bool IsNumber( char Value )
      > > {
      > > if (Regex.IsMatch( Value.ToString( ), @"^[0-9]+$" ))
      > > {
      > > return true;
      > > }
      > > else return false;
      > > }
      > >
      > > I think, if I have to repat this some thousands time, it
      > > could become very time consuming.[/color]
      >
      > While regular expressions are very powerful, they're not very fast for
      > doing simple thing - and you're creating a new string for each call as
      > well. Far simpler and faster is:
      >
      > public static bool IsNumber (char value)
      > {
      > return (value>='0' && value <='9');
      > }
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: How can I do this without Regex ?

        Tim,
        In addition to Jon's comments.

        Is there a reason you are not using the static Char.IsDigit or Char.IsNumber
        function?
        [color=blue]
        > public static bool IsNumber( char Value )
        > {[/color]
        return Char.IsDigit(Va lue);[color=blue]
        > }[/color]

        Char has a number of other static functions that check the character's
        class.

        FWIW, when I have a static method that is checking a 'constant' regex, I
        normally create a static Regex field, then use that. This way the regex
        object itself is not continueally recreated. I even consider making this
        Regex compiled with the RegexOptions.Co mpiled option, to get even a little
        more performance out of it. Seeing as its static, there is only going to be
        one anyway.

        Something like:

        static readonly Regex thePattern = new Regex(@"^[0-9]+$",
        RegexOptions.Co mpiled);
        [color=blue]
        > public static bool IsNumber( string value )
        > {[/color]
        return thePattern.IsMa tch(value);[color=blue]
        > }[/color]

        Hope this helps
        Jay


        "Tim Conner" <TimConner@disc ussions.microso ft.com> wrote in message
        news:08da01c39a c8$64941970$a10 1280a@phx.gbl.. .[color=blue]
        >
        > Is there a way to write a faster function ?
        >
        > public static bool IsNumber( char Value )
        > {
        > if (Regex.IsMatch( Value.ToString( ), @"^[0-9]+$" ))
        > {
        > return true;
        > }
        > else return false;
        > }
        >
        > I think, if I have to repat this some thousands time, it
        > could become very time consuming.
        >
        > Thanks in advance,
        >
        > P.S.
        > Is there in C# something similar to Delphi's sets ?
        >[/color]


        Comment

        • Tim Conner

          #5
          Re: How can I do this without Regex ?


          I have another similar routines, for check alpha numeric,
          and to test for integers and floats. So, I did that just
          by inertia <g>. I may change this routine since it is very
          straight forward, numbers are numbers, so no more to check
          here.

          But for my other routines, I think I do need regex. For
          example the following :

          public static bool IsFloat( string Value)
          {
          if (Regex.IsMatch( Value.ToString( ), @"^[+-]?([0-9]*\.?
          [0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$" ))
          {
          return true;
          }
          else return false;

          }


          I am using the static IsMatch of Regex. So I think I am
          not recreating a regex object each time.
          But your code seems slightly different.
          But I am not sure about "that" difference <g>.
          Would you mind to explain a bit more on this ?


          Thanks in advance,






          [color=blue]
          >-----Original Message-----
          >Tim,
          >In addition to Jon's comments.
          >
          >Is there a reason you are not using the static[/color]
          Char.IsDigit or Char.IsNumber[color=blue]
          >function?
          >[color=green]
          >> public static bool IsNumber( char Value )
          >> {[/color]
          > return Char.IsDigit(Va lue);[color=green]
          >> }[/color]
          >
          >Char has a number of other static functions that check[/color]
          the character's[color=blue]
          >class.
          >
          >FWIW, when I have a static method that is checking[/color]
          a 'constant' regex, I[color=blue]
          >normally create a static Regex field, then use that. This[/color]
          way the regex[color=blue]
          >object itself is not continueally recreated. I even[/color]
          consider making this[color=blue]
          >Regex compiled with the RegexOptions.Co mpiled option, to[/color]
          get even a little[color=blue]
          >more performance out of it. Seeing as its static, there[/color]
          is only going to be[color=blue]
          >one anyway.
          >
          >Something like:
          >
          > static readonly Regex thePattern = new Regex(@"^[0-9][/color]
          +$",[color=blue]
          >RegexOptions.C ompiled);
          >[color=green]
          >> public static bool IsNumber( string value )
          >> {[/color]
          > return thePattern.IsMa tch(value);[color=green]
          >> }[/color]
          >
          >Hope this helps
          >Jay
          >
          >
          >"Tim Conner" <TimConner@disc ussions.microso ft.com> wrote[/color]
          in message[color=blue]
          >news:08da01c39 ac8$64941970$a1 01280a@phx.gbl. ..[color=green]
          >>
          >> Is there a way to write a faster function ?
          >>
          >> public static bool IsNumber( char Value )
          >> {
          >> if (Regex.IsMatch( Value.ToString( ), @"^[0-9][/color][/color]
          +$" ))[color=blue][color=green]
          >> {
          >> return true;
          >> }
          >> else return false;
          >> }
          >>
          >> I think, if I have to repat this some thousands time, it
          >> could become very time consuming.
          >>
          >> Thanks in advance,
          >>
          >> P.S.
          >> Is there in C# something similar to Delphi's sets ?
          >>[/color]
          >
          >
          >.
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: How can I do this without Regex ?

            Tim,[color=blue]
            > I am using the static IsMatch of Regex. So I think I am
            > not recreating a regex object each time.[/color]
            Correct you are not explicitly creating a regex object each time, however
            the static method IS implicitly creating a regex object each time for you.
            Read the description of the static IsMatch carefully!



            The Remarks state: "The two static IsMatch methods are equivalent to
            constructing a Regex object with the specified regular expression pattern
            and calling the instance method IsMatch. The static methods are provided to
            allow an isolated, single use of a regular expression without explicitly
            creating a Regex object."

            Note "isolated, single use" and "equivalent to constructing a regex object"
            in the above statement. I would expect your IsFloat method to be called a
            number of different times from a number of different places...
            [color=blue]
            > But your code seems slightly different.
            > But I am not sure about "that" difference <g>.
            > Would you mind to explain a bit more on this ?[/color]
            What do you mean different? Do you mean I simply return the bool value,
            rather then checking for true and returning true?

            you effectively have:

            bool rc = Regex.IsMath(.. .)

            if (rc)
            return true
            else
            return false;

            where as I effectively have:

            bool rc = Regex.IsMath(.. .)

            return rc;

            I used an explaining variable in the above sample, however they are the
            same.

            Or that you were passing a char, I modified it to pass a string.

            Or that I created an object, and use an instance method of the object?

            In your IsFloat routine, I would simply use:

            private static readonly Regex floatPattern = new
            Regex(@"^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$",
            RegexOptions.Co mpiled);

            public static bool IsFloat(string value)
            {
            return floatPattern.Is Match(value)
            }

            floatPattern is:
            - private : as its an implementation detail
            - static : as we only need one instance of it
            - readonly : as we don't need to change it after its initialized

            IsFloat uses the above Regex to check for a match, value is already a
            string, so we do not need to call ToString a second time. Seeing as
            Regex.IsMatch returns a bool, we can simply return this bool. IsFloat
            encapsulates the Regex object.

            Hope this helps
            Jay



            "Tim Conner" <TimConner@disc ussions.microso ft.com> wrote in message
            news:09f401c39b 1c$7506b7f0$a60 1280a@phx.gbl.. .[color=blue]
            >
            > I have another similar routines, for check alpha numeric,
            > and to test for integers and floats. So, I did that just
            > by inertia <g>. I may change this routine since it is very
            > straight forward, numbers are numbers, so no more to check
            > here.
            >
            > But for my other routines, I think I do need regex. For
            > example the following :
            >
            > public static bool IsFloat( string Value)
            > {
            > if (Regex.IsMatch( Value.ToString( ), @"^[+-]?([0-9]*\.?
            > [0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$" ))
            > {
            > return true;
            > }
            > else return false;
            >
            > }
            >
            >
            > I am using the static IsMatch of Regex. So I think I am
            > not recreating a regex object each time.
            > But your code seems slightly different.
            > But I am not sure about "that" difference <g>.
            > Would you mind to explain a bit more on this ?
            >
            >
            > Thanks in advance,
            >
            >
            >
            >
            >
            >
            >[color=green]
            > >-----Original Message-----
            > >Tim,
            > >In addition to Jon's comments.
            > >
            > >Is there a reason you are not using the static[/color]
            > Char.IsDigit or Char.IsNumber[color=green]
            > >function?
            > >[color=darkred]
            > >> public static bool IsNumber( char Value )
            > >> {[/color]
            > > return Char.IsDigit(Va lue);[color=darkred]
            > >> }[/color]
            > >
            > >Char has a number of other static functions that check[/color]
            > the character's[color=green]
            > >class.
            > >
            > >FWIW, when I have a static method that is checking[/color]
            > a 'constant' regex, I[color=green]
            > >normally create a static Regex field, then use that. This[/color]
            > way the regex[color=green]
            > >object itself is not continueally recreated. I even[/color]
            > consider making this[color=green]
            > >Regex compiled with the RegexOptions.Co mpiled option, to[/color]
            > get even a little[color=green]
            > >more performance out of it. Seeing as its static, there[/color]
            > is only going to be[color=green]
            > >one anyway.
            > >
            > >Something like:
            > >
            > > static readonly Regex thePattern = new Regex(@"^[0-9][/color]
            > +$",[color=green]
            > >RegexOptions.C ompiled);
            > >[color=darkred]
            > >> public static bool IsNumber( string value )
            > >> {[/color]
            > > return thePattern.IsMa tch(value);[color=darkred]
            > >> }[/color]
            > >
            > >Hope this helps
            > >Jay
            > >
            > >
            > >"Tim Conner" <TimConner@disc ussions.microso ft.com> wrote[/color]
            > in message[color=green]
            > >news:08da01c39 ac8$64941970$a1 01280a@phx.gbl. ..[color=darkred]
            > >>
            > >> Is there a way to write a faster function ?
            > >>
            > >> public static bool IsNumber( char Value )
            > >> {
            > >> if (Regex.IsMatch( Value.ToString( ), @"^[0-9][/color][/color]
            > +$" ))[color=green][color=darkred]
            > >> {
            > >> return true;
            > >> }
            > >> else return false;
            > >> }
            > >>
            > >> I think, if I have to repat this some thousands time, it
            > >> could become very time consuming.
            > >>
            > >> Thanks in advance,
            > >>
            > >> P.S.
            > >> Is there in C# something similar to Delphi's sets ?
            > >>[/color]
            > >
            > >
            > >.
            > >[/color][/color]


            Comment

            • Mihai N.

              #7
              Re: How can I do this without Regex ?

              > I have another similar routines, for check alpha numeric,[color=blue]
              > and to test for integers and floats. So, I did that just
              > by inertia <g>. I may change this routine since it is very
              > straight forward, numbers are numbers, so no more to check
              > here.[/color]
              Don't validate to much.
              Some day the marketing team will decide they can sell
              this in Europe and you have to translate it.
              And suddenly you validations go down the drain.
              Your alpha numeric will not accept accented characters,
              your float will not accept numbers where the
              decimal point is comma, etc.

              Mihai

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: How can I do this without Regex ?

                Bsiang Tan <tbsiang1982@ho tmail.com> wrote:[color=blue]
                > Why don't you try the framework given method ?
                > Check the Char.IsNumber( ) ?
                >
                > For example :
                > Console.WriteLi ne(Char.IsNumbe r('8')); // Output: "True"[/color]

                Actually, what was asked for is really Char.IsDigit (ie just 0-9) - but
                I wouldn't have spotted that if you hadn't raised it :)

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • codymanix

                  #9
                  Re: How can I do this without Regex ?

                  The simplest would be to do a float.Parse(myS tring) and check wheather a
                  exception occures or not.

                  --
                  cody

                  [Freeware, Games and Humor]
                  www.deutronium.de.vu || www.deutronium.tk


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: How can I do this without Regex ?

                    codymanix <dont.spam.me.d eutronium@gmx.d e> wrote:[color=blue]
                    > The simplest would be to do a float.Parse(myS tring) and check wheather a
                    > exception occures or not.[/color]

                    And the fastest would be to do some preliminary validation before
                    calling float.Parse. Again, writing a small routine which knows about
                    floating point formats is likely to be fairly simple, and significantly
                    faster than a regex.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    Working...