0

I have a url that contains a ~, and I need to handle the encoded version of this character %7E.

Currently, we have 2 rules to handle this case.

^/folder/([\w-]+)~(\d*).aspx$
^/folder/([\w-]+)%7E(\d*).aspx$

Can I combine this into one rule? I tried the following but it does not work:

^/folder/([\w-]+)[~|%7E](\d*).aspx$

Any help with this rule is appreciated.

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Brian Salta
  • 1,576
  • 1
  • 10
  • 18

1 Answers1

1

Maybe you would like to try

^/folder/([\w-]+)(~|%7E)(\d*)\.aspx$

or

^/folder/([\w-]+)(?:~|%7E)(\d*)\.aspx$

to keep your capture groups the same if you regex engine supports this syntax.

This […] constructs a character class, not a group of alternatives. So [~|%7E] means "either a literal ~, |, %, 7 or E.

You have to use parens for that. And you should escape the . in \.aspx because . means "any character" in regexes.

amon
  • 57,091
  • 2
  • 89
  • 149
  • That works, but changes my existing matches. So what was $2 is now $3. Is there a way to keep the matches the same? – Brian Salta Aug 03 '12 at 17:39
  • @BrianSalta updated my answer, but you could alternatively just change all occurences from `$2` to `$3`. – amon Aug 03 '12 at 17:55
  • That works, I appreciated the updated answer. Our rewrite file is quite lengthy, would take a while to update all the new capture groups. – Brian Salta Aug 03 '12 at 18:03