1

I'm trying to do a search and replace (for multiple chars) in the following string:

VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&

One or more of these characters: %3D, %2F, %2B, %23, can be found anywhere (beginning, middle, or end of the string) and ideally, I'd like to search for all of them at once (using one regex) and replace them with = or / or + or # respectively, then return the final string.

Example 1:

VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&

Should return

VAR=/lkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA+7G3e8=&

Example 2:

VAR=s2P0n6I%2Flonpj6uCKvYn8PCjp%2F4PUE2TPsltCdmA%3DRQPY%3D&

Should return

VAR=s2P0n6I/lonpj6uCKvYn8PCjp/4PUE2TPsltCdmA=RQPY=&
11
  • 3
    What language are you doing this in? Your language probably has a native way to urldecode. Commented Sep 15, 2013 at 20:18
  • I'm pretty sure regex can't do this. Commented Sep 15, 2013 at 20:21
  • A single regex? You'll need a function to pick which set of chars to replace with the corresponding set of chars as well. Commented Sep 15, 2013 at 20:21
  • Python, BUT what I'm looking for is a simple expression, not lines of code or scripts. Something like this:'VAR':\s*'([^']*)' something that will step through the string, find and replace those characters. I found this, but I don't know how to apply it to my case: stackoverflow.com/questions/14059129/… Commented Sep 15, 2013 at 20:24
  • That question is different. The variables replaced are already in the string, while yours aren't. Commented Sep 15, 2013 at 20:29

5 Answers 5

2

I'm not convinced you need regex for this, but it's fairly easy to do with Python:

x = 'VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&'

import re

MAPPING = { 
    '%3D': '=',
    '%2F': '/',
    '%2B': '+',
    '%23': '#',
}

def replace(match):
    return MAPPING[match.group(0)]

print x
print re.sub('%[A-Z0-9]{2}', replace, x)

Output:

VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&
VAR=/lkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA+7G3e8=&
Sign up to request clarification or add additional context in comments.

3 Comments

That's great and I could follow what you did, but is there a way to do this using some slick regex or is it just not possible? Cheers Mate!
Look at my answer, is that what you call "slick"?
@MyPM: since there is no simple metohd to convert the hex numbers into characters with the Python regex engine, no. You will always need to use a function like Ofir and I did or use multiple replaces like CodeChords did.
2

There is no need for a regex to do that in your example. A simple replace method will do:

def rep(s):
    for pat, txt in [['%2F','/'], ['%2B','+'], ['%3D','='], ['%23','#']]:
        s = s.replace(pat, txt)
    return s

Comments

2

I'm also not convinced you need regex, but there's a better way to do url-decode with regex. Basically you need that every string in the pattern of %XX will be converted into the char it represents. This can be done with re.sub() like so:

>>> VAR="%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&"
>>> re.sub(r'%..', lambda x: chr(int(x.group()[1:], 16)), VAR)
'/lkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA+7G3e8=&'

Enjoy.

1 Comment

Thank You Guys! I will try to get this to work and report back! Cheers!
1
var = "VAR=s2P0n6I%2Flonpj6uCKvYn8PCjp%2F4PUE2TPsltCdmA%3DRQPY%3D&"
var = var.replace("%2F", "/")
var = var.replace("%2B", "+")
var = var.replace("%3D", "=")

but you got same result with urllib2.unquote

import urllib2
var = "VAR=s2P0n6I%2Flonpj6uCKvYn8PCjp%2F4PUE2TPsltCdmA%3DRQPY%3D&"
var = urllib2.unquote(var)

Comments

-1

This can't be done with a regex because there's no way to write any kind of conditional inside of a regex. Regular expressions can only answer the question "Does this string match this pattern?" and not perform the operation "If this string matches this pattern, replace part of it with this. If it matches this pattern, replace it with this. etc..."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.