Results 1 to 33 of 33

Thread: Regular Expression [ RESOLVED ]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Regular Expression [ RESOLVED ]

    I'm trying to add a regular expression that does not match
    if the string begins 123. How can I do this? Any other number beginning will
    be a match, such as 133, 124, etc.
    Last edited by dogboydelta; Mar 6th, 2006 at 04:47 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Regular Expression

    [^123] can be used so that if it starts with 123, no match.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    That's what I tried first, but [^123] matches not any of the characters, so if the
    string starts with a 1, 2 or 3 it will be rejected. The [^...] only matches one character. I need to reject a string that starts exactly 123. If it starts 321 or 132 it should be accepted. I tried [^1][^2][^3] but that didn't work either, as it matches 3 characters, but if any one is out of line it will reject ( ie. 943 would be rejected ).

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Regular Expression

    Is it always a string of numbers?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    yes.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Regular Expression

    I'll try to figure out the regex for this, but couldn't you just use a simple string.IndexOf() operation to figure out whether the first three characters are 123?

  7. #7
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    (?!123)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    Well, at least that's it in theory but now that I test it - it doesn't seem to work in practice. Gimme a min. I'll get it for you.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    OK, the dot-net syntax is slightly different than I am used to, but here is what it works out to be:

    ((?!123)\d{3})
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    Its probably going to be used in a RegularExpressionValidator ( client side ) aspx file, so it pretty much has to be a straight regularexpression, cant use any string methods. I will try that latest regex and see what happens.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Regular Expression

    I can't say I'm familiar with ?!, is that effectively the same as ^?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    ((?!123)\d{3})

    I tried this one - its on the right track. It accepted 124 and rejected 123. The problem is it accepted 123456. Any other suggestions?

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Regular Expression

    Try

    ^(?!123)\d*$

  14. #14
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    OK, I was thinking it was only a three digit field.

    What you are looking for then is:

    ^((?!123)\d*)

    Mendhak:
    (?! means "check what is coming up next and make sure it does NOT match this pattern"

    (^ is not something I am familiar with.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    I got it, you have to add the begin match ^ at the beginning ( otherwise you can match "NOT 123" anywhere in the string and it will be a match.

    This works:

    ^((?!123)\d{3})

  16. #16
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    All three of us jumped on the answer bandwagon at the same time. =D
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    Is there an AND in Regular Expressions? What if I want it to not start with 123 and also not start with 789?

  18. #18
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    ^((?!123)(?!789)\d*)$
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  19. #19
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Regular Expression

    I understand ?! now. I was using ^, which is apparently regular-regular expressions, but .NET probably confuses it with the anchor start of the string, so it expects ?!.

    Fuhh....

  20. #20
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    Well, ?! is pretty standard really. =)

    But hey, Regular Expressions themselves are for people who are nutjobs (like us) to begin with. I mean, look at what we just wrote and the fact is even makes sense to someone...
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    Now I'm having another problem. ^((?!123)\d{3}) works great when I test it
    using the Regex methods in vb, but when I plug it into a regularexpression validator, it rejects any number I type in at all. What's the deal? I'm using ASP.NET 1.1 , is there some compatibility issue?

  22. #22
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    No, if there is anything it is the validator itself since to test my regular expressions I use VB.Net 1.1 using a program I wrote myself.

    Try taking out the outside set of parens:
    ^(?!123)\d*$
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    I have a program too. I try it in the program and it works, then I cut and paste the expression into the web site and it doesn't work - it rejects everything. There are two regularexpression validators on the same textbox, is that a problem? I'm just guessing at this point, I have no idea why it wont work in the Regex Validator.

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    Oh I just realized - Validators get converted into Javascript, which may be slightly different as far as Regular Expression syntax compared to .NET, isn't that right?

  25. #25
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Regular Expression

    Do you need JS regex code or .NET crap? If the former I can help.

    If you just want it to match any string that does not start with "123", something like this shoudl work:

    /(^(123))(.*)/
    Last edited by penagate; Mar 6th, 2006 at 03:18 PM. Reason: Merged posts.

  26. #26
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    Quote Originally Posted by penagate
    ... or .NET crap? ...
    You are on a .Net forum.

    Most of us swear by this language. Be careful.

    As for the question, there is no difference between the ASP.Net version and the Javascript version.

    The "/" that was just presented is part of the Javascript language, not part of the Regular expression.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  27. #27
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    For example of what I mean on the javascript argument, try the following:

    assert(/^(?!123)\d*$/.test("123456"));

    vs.

    assert(/^(?!123)\d*$/.test("456123"));
    Last edited by Lord_Rat; Mar 6th, 2006 at 03:22 PM.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  28. #28
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    Forgot that assert is my own function. Here would be the entire code to test:

    Code:
    <script lang="javascript">
    function assert(testVal)
    {
     if(!testVal) alert("Assert failed!");
    }
    
    function A1(){
             assert(/^(?!123)\d*$/.test("123456"));
    }
    
    function A2(){
             assert(/^(?!123)\d*$/.test("456123"));
    }
    </script>
    <input type="button" onclick="A1();" value="1" />
    <input type="button" onclick="A2();" value="2" />
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    Ok, here's my regex:

    ^((?!\s*\(?\s*123)(?!\s*\(?\s*0))

    It rejects a string that starts 123 , OR starts 0, and accepts all others. It works when I do it manually, in Javascript and in .NET Regex( ) methods, but when I plug it into a RegularExpressionValidator it rejects ALL strings. Can someone try that - plug it into a regularexpressionvalidator in ASP.NET 1.1 and see if I'm crazy or not?

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    This is what is in the WebUIValidation, in case it helps:

    function RegularExpressionValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
    return true;
    var rx = new RegExp(val.validationexpression);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
    }

  31. #31
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression

    Try this one:

    ^\s*(?!123)(?!0)(.*)$

    Does that work?
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Regular Expression

    Yes it does. I don't know why the WebUIValidation uses RegExp.exec instead of
    RegExp.test, which just returns a true or false. Anyway, works good.

  33. #33
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Regular Expression [ RESOLVED ]

    OK.

    One thing I did notice with the expression you pasted before was that it wasn't closed properly. There should have been four ")" at the end.

    But glad it works. It might need a set of parens around the \s* part too. I wasn't sure.

    Enter a few spaces before your number and then a valid number. If it fails (with a valid number) then that is probably why.

    Which would make it:
    ^(\s*)(?!123)(?!0)(.*)$
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width