Solved

Regex to Match Anything but the Number 23, isapai rewrite

Posted on 2014-03-06
11
333 Views
Last Modified: 2014-03-07
Hello

I am trying to write a regular expression like the following to redirect query string id to 23 if it receives anything but 23:

RewriteRule ^manual-treadmill.aspx\?ID=[^23]+.* manual-treadmill.aspx?ID=23 [NC,O,R=301]

It works great unless they put a number that starts with a two or three. So it is basically breaking if they put:

2
3
32
233

but it works if they put
1
4
5
6
7
8
9
23
11
41
51
61
71
81
91
etc.

Can someone please tell me how to update this only redirects if they don't put 23 in that order?

Thanks in advance,
Shawn
0
Comment
Question by:smower
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39911277
If you want to redirect query string id to 23 if it receives anything but 23, why not redirect everything to 23 and use RewriteCond to skip the rule if it is already 23?
0
 

Author Comment

by:smower
ID: 39911331
Not sure how to do that. I have very limited understanding of ISAPI rewrite. I have a slight knowledge of regular expressions but I was trying to figure out how to do the regular expression to find any query ID except for 23.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39911338
if your regular expression engine does not support (?!) then you might use (.?$|[^2][^3]|...)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:smower
ID: 39911646
Thanks, that fixes the few example numbers but is still not quite getting everything for example
123
2333
-123
322

I need it to capture everything except 23 only.

Thank you,
Shawn
0
 
LVL 84

Expert Comment

by:ozo
ID: 39911649
I don't know why 123 and 322 would not match, but if the expression is anchored, you may have to change ... to ...+
0
 

Author Comment

by:smower
ID: 39911658
^manual-treadmill.aspx\?ID=(.?$|[^2][^3]|...+)

it's still not working for me. I am testing it on:
http://www.regexr.com/
0
 
LVL 84

Expert Comment

by:ozo
ID: 39911677
In what way is it not working?  It seems to do what I expect.

By the way http://www.regexr.com/ seems to support (?!23)
0
 

Author Comment

by:smower
ID: 39912654
it only partially matches. For example:
123 or 1234 or 12345  or 123456 or '123 or 123" or -123 or 123- etc. - only catches the first couple numbers or characters.
33 doesn't get captured
22 doesn't get captured
anyway these are just a couple of examples. I need it to capture everything except 23 exactly.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39913301
Sorry, I thought you only needed a match, not a capture.
You can make the ...+ the first option:
(...+|.?$|[^2][^3])
Or, since http://www.regexr.com/ supports (!?)
(?!23$)(.*)
0
 

Author Comment

by:smower
ID: 39913365
Awesome, thank you so much.  This one worked!: (?!23$)(.*)

the other one mostly worked: (...+|.?$|[^2][^3])
However it would still have issues with:
33
20
21
22
24
25
26
27
28
29

but since the other one works I will just use that.
Thank you!
Shawn
0
 

Author Closing Comment

by:smower
ID: 39913367
(?!23$)(.*)  works.
(...+|.?$|[^2][^3]) partially works.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question