Solved

Regex to Match Anything but the Number 23, isapai rewrite

Posted on 2014-03-06
11
329 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

777 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