Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Regex to Match Anything but the Number 23, isapai rewrite

Posted on 2014-03-06
11
332 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular expression patterns 2 77
numeric only entry in text inpu 9 44
Regex to filter only IP's? 4 58
Regular Expression 1-100 with two digits 15 59
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…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

839 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