locked
Regular expressions RRS feed

  • Question

  • Hi,

    I am having trouble with regular expressions in vb.net 3.51

    here is my code.

         --this one checks for alphas, upper & lower case and numbers

            Dim isMatch1 As Boolean = System.Text.RegularExpressions.Regex.IsMatch("asdasddsa", "^[a-zA-Z0-9]$")

    --this one strings between 5 and 40 characters

            Dim isMatch2 As Boolean = System.Text.RegularExpressions.Regex.IsMatch("abasasdsdc", "^{5,40}$")

    I expected both to produce true, but both produce false

    I found these on the web, they look pretty simple but dont seem to work

    I have tried other (regex) methods and get unexpected results


    Friday, May 11, 2012 9:02 AM

Answers

  • First I recommend Expresso as a fantastic workbench for testing and creating regular expressions.  Registration is required, but free last time I checked.  With it you can quickly test and tweek until it's correct.

    For your alphanumeric test try  "^[a-zA-Z0-9]+$"   Without the + your expresison is testing for a single character.  The + implies 1 or more. If an empty string is valid use * instead of + which implies zero or more.

    For between 5 and 40 try "^.{5,40}$"  Without the dot you are not looking for anything.  A . matches any character. 

    You can combine both tests and look for 5 to 40 alphanumeric via "^[a-zA-Z0-9]{5,40}$"

     @Matteo ... if the only tool in your box is a hammer, everyting starts to look like a nail ;-)


    This signature unintentionally left blank.

    • Proposed as answer by Mike Feng Monday, May 14, 2012 10:02 AM
    • Marked as answer by Mike Feng Monday, May 21, 2012 11:16 AM
    Friday, May 11, 2012 10:30 AM
  • For me this method is clearer:
    Public Function IsValid(text As String, min As Integer, max As Integer) As Boolean
    	Return _
    		(text.Length >= min AndAlso text.Length <= max)	AndAlso _
    		text.All(Function(c) (Char.IsLetterOrDigit(c)))
    End Function

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    • Proposed as answer by Mike Feng Monday, May 14, 2012 10:01 AM
    • Marked as answer by Mike Feng Monday, May 21, 2012 11:16 AM
    Friday, May 11, 2012 11:11 AM

All replies

  • I suggest you to don't use regular expressions, are difficult to write and impossible to debug!

    Use LINQ to solve the problem.

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    Friday, May 11, 2012 9:21 AM
  • First I recommend Expresso as a fantastic workbench for testing and creating regular expressions.  Registration is required, but free last time I checked.  With it you can quickly test and tweek until it's correct.

    For your alphanumeric test try  "^[a-zA-Z0-9]+$"   Without the + your expresison is testing for a single character.  The + implies 1 or more. If an empty string is valid use * instead of + which implies zero or more.

    For between 5 and 40 try "^.{5,40}$"  Without the dot you are not looking for anything.  A . matches any character. 

    You can combine both tests and look for 5 to 40 alphanumeric via "^[a-zA-Z0-9]{5,40}$"

     @Matteo ... if the only tool in your box is a hammer, everyting starts to look like a nail ;-)


    This signature unintentionally left blank.

    • Proposed as answer by Mike Feng Monday, May 14, 2012 10:02 AM
    • Marked as answer by Mike Feng Monday, May 21, 2012 11:16 AM
    Friday, May 11, 2012 10:30 AM
  • You're right Nick :). But, why you want have only RegEx tool, in your box? :)

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    Friday, May 11, 2012 10:47 AM
  • For me this method is clearer:
    Public Function IsValid(text As String, min As Integer, max As Integer) As Boolean
    	Return _
    		(text.Length >= min AndAlso text.Length <= max)	AndAlso _
    		text.All(Function(c) (Char.IsLetterOrDigit(c)))
    End Function

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    • Proposed as answer by Mike Feng Monday, May 14, 2012 10:01 AM
    • Marked as answer by Mike Feng Monday, May 21, 2012 11:16 AM
    Friday, May 11, 2012 11:11 AM
  • Hi,

    Do as follows

     1) Dim isMatch1 As Boolean = System.Text.RegularExpressions.Regex.IsMatch("asdasddsa", "^[a-zA-Z0-9]+$")

    2)  Dim isMatch2 As Boolean = System.Text.RegularExpressions.Regex.IsMatch("abasasdsdc", "^[a-zA-Z0-9]{5,40}$")


    PS.Shakeer Hussain

    Friday, May 11, 2012 11:19 AM
  • @Matteo

    I have to agree with Nick. Learning regular expression where it is right tool gives you more than just using helper method to validate every possible case there might be. The question was about regular expression, so it might be important to get answer to the problem in reqular expression and give option for other solution like you did. Don't doom something hard to debug and difficult to write if you don't know them. LINQ queries are also hard to write and debug and more so if you start to validate complex strings with them.

    Friday, May 11, 2012 11:23 AM
  • Answer with an option, is an answer, because the OP can don't know how to use LINQ.

    I can use RegEx, but you can't compare debug of a LINQ query (that is possible) versus debug a of a  RegEx, that is not possible.

    If the complexity of the condition augments, the RegEx complexity augments more, infact there are tools that help to write them and the only way to check if the RegEx is correct is to use test cases, this is not a developer friendly approach.

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    Friday, May 11, 2012 11:35 AM
  • LOL, I wasn't intending to start an argument.  Just for giggles however  

    @Matteo I admit to being ignorant about LINQ ...comes from working a product trying very hard to maintain backward combatability. What LINQ query would you use to validate and/or normalize a phone number under the following conditions.

    1. May or may not have separators (valid separators may be '-' ',' or '.').
    2. May or may not have parenthesis around area code.
    3. May or may not have a country code.

    This common task is child's play with a regular expresson (google it).  How would you go about it with LINQ?


    This signature unintentionally left blank.


    • Edited by Nick F. _ Friday, May 11, 2012 1:57 PM removed the redundant redundant word
    Friday, May 11, 2012 1:56 PM
  • Of course RegEx are powerful but also complex.

    Are you agree? :)

    Using LINQ you have to think in sets and split the problem, the power of this approach is "divide et impera". The string at this point became a set of tokens that are "objects" and can be validated separately.

    The problem of RegEx is that if the requirements change or is it isn't correct for a new use case, changing it you can broke all the validation and you can't understand why :).

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    Friday, May 11, 2012 2:19 PM
  • I understand matteo what your talking about. If something changes in case like person inputs a wrong character thats not in the regular expression but lets say character box for windows which has millions of possibilities its impossible probably on both counts unless you go off the ascii code of that character or ` is used in a phone number by accident and its not setup to do that.

    But matteo and nick i believe the same problem is relevent to both linq and regular expressions because you just cant find every case and its hard to make code changes the code its hard in regex but could be in linq with complex expressions.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://jefferycarlsonblog.blogspot.com/


    Friday, May 11, 2012 4:13 PM
  • No I'm not saying this.

    I'm saying that:
    1. if a requirement (condition to test) changes, change the RegEx could be more difficult than OO code (RegEx is not OOP)
    2. if a RegEx is wrong is difficult to understand what change
    3. is not possible to debug a RegEx
    4. is not possible to dived problem in little problems, easier to solve

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    Friday, May 11, 2012 4:26 PM
  • Hi Michael,

    Do you have any updates?

    How about your issue now?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 17, 2012 11:32 AM
  • Yes both are true.  I was genuinely curious how you'd meet said requirements, though reading it now it seems more like a challenge.

    Cheers.


    This signature unintentionally left blank.

    Friday, May 18, 2012 6:19 PM