locked
Regular Expression: Not Equal RRS feed

  • Question

  • I am looking for a regular expression which will find a 5-digit ID that is NOT 12345.

    Example:
    "id-12345"   should NOT match 

    "id-23456"  - should match 
    "id-34567"  - etc. should match 

    I tried using a "Negative Lookbehind" Regular Expression with Microsoft Expression Web 4:  
    "id-{[0-9]^5}(?<!12345)" and also \"id\-{[0-9]^5}(?<!12345)\"
    but it does not find a match:

    The simple "equal" match works:
    "id-{[0-9]^5}"  finds all matches in the 3 examples above.

    PS: ASP.net does support "Negative Lookbehind"
    http://msdn.microsoft.com/en-us/library/ms972966.aspx#regexnet_topic4

    Anything I am missing?

    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU

    Sunday, May 19, 2013 10:21 PM

Answers

  • "Yes, I am trying it in the EW Search/Replace dialogue."

    I wish you had said that in the beginnning instead of referring to asp.net supporting lookaround syntax, giving the impression that you were talking about your site (asp.net validator, using regex inside your site, etc.).

    EW's search function does not support lookaround syntax.  Read EW's help documentation on regular expressions.  You may not be able to do what you are trying to do in one pass.

    You can certainly search and replace id-12345 itself, replacing it with "MYDUMMYID", then do your search for any id- followed by 5 digits, do what you want with them, and when done, search for "MYDUMMYID" and replace it back to  "id-12345".




    • Edited by KathyW2 Monday, May 20, 2013 6:58 PM
    • Marked as answer by Platini3 Monday, May 20, 2013 7:24 PM
    Monday, May 20, 2013 6:41 PM
  • I found another solution:
    ~x   Prevent match    prevents a match when x appears at this point in the expression.

    id-~(12345)

    will give a match for: 

    id-23456
    id-34567
    id-22345
    id-13345
    id-11111
    id-22222
    id-33333

    but not:
    id-12345

    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU


    • Marked as answer by Platini3 Wednesday, May 22, 2013 4:37 PM
    • Edited by Platini3 Wednesday, May 22, 2013 4:38 PM
    Wednesday, May 22, 2013 4:37 PM

All replies

  • You are over thinking this.  :)   You know that you want to match exacty 5 digits, and for each of those you know exactly which digits are allowed.  That makes this trivial. Simply enumerate the allowed digits in each of the five places:

    id-[02-9][013-9][0-24-9][0-35-9][0-46-9]

    • Marked as answer by Platini3 Sunday, May 19, 2013 11:04 PM
    • Unmarked as answer by Platini3 Sunday, May 19, 2013 11:53 PM
    Sunday, May 19, 2013 10:46 PM
  • Wow!

    You are a genius!

    The power of simplicity! 

    :)

    Thank you so much! 


    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU

    Sunday, May 19, 2013 11:03 PM
  • Ooops,

    I spoke to soon.

    It won't work. It doesn't flag:

    "id-22345"
    "id-13345"
    "id-11111"
    "id-22222"
    "id-33333"

    etc.


    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU


    • Edited by Platini3 Sunday, May 19, 2013 11:54 PM
    Sunday, May 19, 2013 11:52 PM
  • Sorry about posting too fast.

    But .. no time to pursue it further.

    Monday, May 20, 2013 4:44 AM
  • I tried "lookahead": 

    id-(?!12345)[0-9]{5}

    and "lookbehind":

    id-[0-9]{5}(?<!12345)

    Both are syntactically correct. 

    My conclusion so far is that even though it is supported by ASP.net, Expression Web may not support  "lookahead" and "lookbehind" regular expressions?


    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU

    Monday, May 20, 2013 3:04 PM
  • Where are you trying this?
    If in the EW Search/Replace dialogue - regular expressions are extremely limited, and uses a different syntax. (See "Regular Expressions for finding text" in the Help)
     
    If in asp.net (or whatever) code, then Expression Web support is irrelevant - the server runs the code, not EW.
    For the code, you can test regular expressions at http://regexlib.com/
     

    Ron Symonds
    Microsoft MVP (Expression Web)

    www.rxs-enterprises.org/fp
    Monday, May 20, 2013 3:52 PM
  • That looks to be pretty hard with a regular expression.

    It would be *much* easier to to that with a bit of VB or C# code that tested for that particular value

    If (the input text) = "ID-12345" Then (your error message)



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Database Tutorials
    Ownertrades.com -- Created with Expression, VWDExress, SQL Express, and ASP.NET
    Arvixe -- My favored web host

    Monday, May 20, 2013 5:02 PM
  • Thank you, Ron.

    Yes, I am trying it in the EW Search/Replace dialogue.
    I looked up "Regular Expressions for finding text" in Help.

    "lookahead"  ?!  and "lookbehind" ?<!  do not seem to be supported.

    I tried [^] matches any character that is not in the set of characters that follows the caret (^):

    "id-[^1][^2][^3][^4][^5]

    but it gives the same results as KathyW2's idea - it will flag 54321 as being different, but it will  not detect 11111 as being different from 12345.

    I guess it just can't be done.

     

    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU

    Monday, May 20, 2013 5:16 PM
  • "Yes, I am trying it in the EW Search/Replace dialogue."

    I wish you had said that in the beginnning instead of referring to asp.net supporting lookaround syntax, giving the impression that you were talking about your site (asp.net validator, using regex inside your site, etc.).

    EW's search function does not support lookaround syntax.  Read EW's help documentation on regular expressions.  You may not be able to do what you are trying to do in one pass.

    You can certainly search and replace id-12345 itself, replacing it with "MYDUMMYID", then do your search for any id- followed by 5 digits, do what you want with them, and when done, search for "MYDUMMYID" and replace it back to  "id-12345".




    • Edited by KathyW2 Monday, May 20, 2013 6:58 PM
    • Marked as answer by Platini3 Monday, May 20, 2013 7:24 PM
    Monday, May 20, 2013 6:41 PM
  •   You may not be able to do what you are trying to do in one pass.

    You can certainly search and replace id-12345 itself, replacing it with "MYDUMMYID", then do your search for any id- followed by 5 digits, do what you want with them, and when done, search for "MYDUMMYID" and replace it back to  "id-12345".




    Thank you KathyW2.

    That solves it!

    I do not mind doing it in 2 passes. 

    It is simple and does the job.


    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU

    Monday, May 20, 2013 7:27 PM
  • Heh -- like Kathy said. I thought you were trying to use it to validate an asp.net  textbox into which a user was typing data ---


    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Database Tutorials
    Ownertrades.com -- Created with Expression, VWDExress, SQL Express, and ASP.NET
    Arvixe -- My favored web host

    Tuesday, May 21, 2013 11:45 AM
  • I found another solution:
    ~x   Prevent match    prevents a match when x appears at this point in the expression.

    id-~(12345)

    will give a match for: 

    id-23456
    id-34567
    id-22345
    id-13345
    id-11111
    id-22222
    id-33333

    but not:
    id-12345

    If you don't have anything nice to say, don't say anything at all. (English Proverb) EW 4.0.1460.0 (SP2) | .NET Framework v4.5.50709 |Win7 Pro 64-bit (SP1) | 16GB RAM | i7 Quad-Core CPU


    • Marked as answer by Platini3 Wednesday, May 22, 2013 4:37 PM
    • Edited by Platini3 Wednesday, May 22, 2013 4:38 PM
    Wednesday, May 22, 2013 4:37 PM