tags:

views:

42

answers:

1

Hey everyone,

I'm looking for a Regex that will take this:

?pm=512862895835

And turn it into this:

?pm=512862895835.png

I'd be running the expression on HTML.

Thanks,

rocky

+3  A: 

You are not very clear, so I'll assume those numbers are a variable.

So your pattern is

?pm=n

I'll also assume

  • you don't want ones with existing .png extensions
  • these may appear anywhere in a string

which means this regex will find them

/\?pm=\d+(?!\.png)/

and replace with

$0.png

alex
Just don't run it twice ;)
Jason McCreary
@Jason The lookahead should fix that :) Thanks.
alex
Alex, thank you for the Regex. ".png" shouldn't be in the search expression though, only in the replacement. I'm basically trying to add .png to the end of a bunch of random numbers.
rocky
@rocky: The "(?!\.png)" part is in the regex to avoid adding ".png" second time to URLs which already have it.
Alexander Prokofyev
@rocky What @Alexander Prokofyev said :) If you don't want the lookahead, see the edits :)
alex
Got it. Thanks fellas!
rocky
Hey Alex, if you're still around... can you tell me what the Perl friendly regex for this would be? Your regex works great on my HTML, but I'm trying to run it on Yahoo! Pipes and it's not working. Thanks!
rocky
@rocky Hello, still around :) That regex should be Perl compatible (well I assume it is because I think it'll work with PHP's `preg_replace()`, and the *preg* stands for **Perl** compatible regular expression). If it doesn't work, try removing the lookahead. Just don't run it twice :)
alex