0

I am new to regex and unable to create a expression to resolve my problem.

I have a string like /abc/def%20hi%28hello%29test. I would like to replace the hexadecimal characters, that is, %20, %28 and %29 with -

I know all three of them have different meanings, but I am fine with replacing all with any single character, I mean - or even . or ' '(space).

So, I would like to get the output as /abc/def-hi-hello-test.

I thought to find % and replace three characters from %[a-zA-Z0-9][a-zA-Z0-9] with -, but doesn't work.

Can some please help me write a regex which just replaces those hexadecimal characters?

HamZa
  • 14,671
  • 11
  • 54
  • 75
vnkotak
  • 129
  • 4
  • 14
  • 4
    What do you mean when you say it "doesn't work"? Using your example, what output does it give? – Michelle Aug 13 '13 at 14:54
  • Your regex will match all your examples... and actually more than you want since hex only goes `a-f`. – Smern Aug 13 '13 at 14:56
  • 2
    We need to know [what you have tried](http://whathaveyoutried.com) in a more specific way - are you writing this in a program? or are you trying to use this in a text editor's search and replace dialogue? Regular Expressions are not language independent - you should consider writing a [Short, Self Contained, Compilable Example](http://sscce.org) so we can better diagnose your problem. – FrankieTheKneeMan Aug 13 '13 at 14:56
  • Seems fine, you could potentially even change it to `%[A-Z0-9]{2}` or `%[A-Z\d]{2}` for easier reading (depending on Regex engine). – Chris Aug 13 '13 at 14:57
  • @Chris You would need to remember the case-insensitive flag for that. – Bernhard Barker Aug 13 '13 at 15:01
  • 2
    Your regex looks fine (except for including `G-Z`, which you were unlikely to have noticed since you just said it "doesn't work", which is really not specific enough). Please construct an [SSCCE](http://sscce.org/) in the language / tool of your choice, which should include an easily reproduceable test case which doesn't work. – Bernhard Barker Aug 13 '13 at 15:03
  • @Dukeling Fair point, I'd assumed he'd grabbed a URL encoded something and he'd always have upper case letters (I'm now not sure why I left the entire range of letters in place). Derp. – Chris Aug 13 '13 at 15:05
  • 1
    I'm under the impression the OP needs the global flag `g` (or equivalent). – Jerry Aug 13 '13 at 15:10
  • 1
    You may need to complicate it quite a bit to be 100% correct - I would guess that `%%28` shouldn't become `%(`, because `%%` is an escaped `%`. – Bernhard Barker Aug 13 '13 at 15:19

2 Answers2

3

Search for:

%[a-fA-F0-9][a-fA-F0-9]

replace with:

-

Example: http://regex101.com/r/qR6xW7

mart1n
  • 5,969
  • 5
  • 46
  • 83
1

It sounds like what you really want to do is decode the URL encoding, and I will bet you that your host language has a facility for that built in. In PHP, that's url_decode.

After you've decoded the URL, then you can modify what it is you want to modify more easily.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152