2

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

2
  • 1
    Is there a set number of digits? More context would help as it is best to anchor a regular expression. Commented Oct 6, 2010 at 0:06
  • Jason, no set number of digits. Just random numbers, which I need to add ".png" to at the end. The only thing each string will have in common is the "?pm=" at the beginning. Commented Oct 6, 2010 at 4:49

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

@Jason The lookahead should fix that :) Thanks.
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: The "(?!\.png)" part is in the regex to avoid adding ".png" second time to URLs which already have it.
@rocky What @Alexander Prokofyev said :) If you don't want the lookahead, see the edits :)
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!
|

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.