Re: Regex.IsMatch help

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Jon Skeet [C# MVP] wrote:

- When trying to find out whether a value will make a good decimal
number, why not just try to convert it into one and see if that
works? You could use the Parse or the TryParse methods on the Single,
Double or Decimal structs, depending on your needs and your framework
version.

Yes, as there's TryParse, that's a good thing to use (it also means you don't need to worry about allowing negatives - something we haven't mentioned yet).

Again, a simpler solution than using regular expressions :)

If it has to be really fast (i.e. it's definitely an application
bottleneck) a hard-coded check is much faster (at least last time I did
some benchmarking) but for actual user input, efficiency probably
wasn't an issue.

I guess the speed difference could be partly due to the fact that you'd code your own check more along the lines of your actual requirements, while the Parse/TryParse methods include support for several formats, plus culture specific things. Or were you saying they are badly coded?



  - A proper expression for your purpose might look like this:

   ^\d*(\.\d+)?$

This matches "3", "4.7" and ".9", but not "1.2.3", ".", "7." or other
funny stuff - only decimal numbers in a common simple syntax.

I was assuming that "7." would be allowed (as it is by Double.Parse). The OP really needs to work out exactly what he wants to allow :)

True. The "7." syntax doesn't add anything to the functionality though, so I wouldn't see the purpose of supporting it.


Now, another thing that you miss (Jon) is the reusability aspect of
regular expressions. For example, if you wanted to find a regex for a more
complicated syntax, like the one involving exponential parts of the
number, you could just search one of the many regex libraries on the net,
like here:

http://www.regexlib.com/Search.aspx?k=decimal%20number

That's true. Of course, you have to make sure you use the right dialect of regular expressions - expressions that will work in .NET may not work in Java and vice versa. (As far as I can see, that site doesn't say which dialect any particular expression is designed for.)

This is usually only a problem with rather complicated stuff like zero width assertions and similar things, where the syntax varies over platforms. Actually, there's a POSIX standard for regular expressions - but it's verbose and ugly, so it's not nice writing expressions to conform with it.


When looking at such a library, it's probably best to find out what people are focused on, so you can guess for which platform they're writing their expressions. For example, if they're advertising Regulator on the front page, .NET isn't too wild a guess ;-)

Of course, the hard-coded check would also be reusable...

I'd rather give someone a good regex - the problem of passing around code snippets that do these kinds of checks is not the passing around in itself. It's more the making small changes to adapt to the target application that quickly breaks things.


Of course regexes break, too. But it's easy to post to a newsgroup with your regex and a few sample inputs and get a good reply quickly. I like to read posts like that, because they tend to be short and to the point. It's much harder posting a complete code snippet with sample inputs in the first place, and it's much harder to read such a post and help that person.


Oliver Sturm -- Expert programming and consulting services available See http://www.sturmnet.org (try /blog as well) .



Relevant Pages

  • Re: Regular Expressions
    ... you will take more time to code and debug the regex than it would ... I know I can hard-code it ... > I haven't used Regular Expressions before so I'm not sure if they can be ...
    (microsoft.public.dotnet.general)
  • Re: Question about Sun JAVAC
    ... I discuss this problem in my book "Design Your Own .Net Language and ... My book contains a discussion of regular expressions which attempts to ... I was last spring engaged in work which involved frequently searching ...
    (comp.programming)
  • Re: Regular Expression to Capture VB Declarations in Visual Studios Find/Replace
    ... I think I'm finally getting these regular expressions. ... Click the 'Help' button to get a list of regex expressions valid ... | in the find/replace dialogs. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regular Expression to Capture VB Declarations in Visual Studios Find/Replace
    ... I think I'm finally getting these regular expressions. ... Click the 'Help' button to get a list of regex expressions valid ... | in the find/replace dialogs. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: bash, grep, and regular expressions
    ... > |them lately using Jeffrey Friedl's book Mastering Regular Expressions ... > specifically requests listing all hidden files and directories, ... > the grep command. ...
    (Debian-User)