Simple regular expression replace for picture URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • motaweb
    New Member
    • May 2010
    • 3

    Simple regular expression replace for picture URL

    I need to replace picture links inside strings like this example :

    blabla http://img146.imagesha ck.us/img126/6348/anything.jpg blabla
    for
    blabla [img:http://img146.imagesha ck.us/img126/6348/anything.jpg] blabla

    I can found them with this regex without problems : ((http(s?):)|([/|.|\w|\s])*\.(?:jpg|gif| png)

    My actual replace is : [img:$&]

    I didn't find a way to put my last bracket at the very end of my url with my replace.

    Thanks for your help
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    so is it showing up like this?

    blabla [img:http:][img://img146.imagesha ck.us/img126/6348/anything.jpg] blabla
    Last edited by jhardman; May 18 '10, 05:09 AM. Reason: second guess

    Comment

    • motaweb
      New Member
      • May 2010
      • 3

      #3
      Originally posted by jhardman
      so is it showing up like this?

      blabla [img:http:][img://img146.imagesha ck.us/img126/6348/anything.jpg] blabla
      Right, exactly what I get.

      I want to remove the ][ between http:// and not repeat img in my string...

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        try this variation, it worked on my test, but I only tested one string.
        Code:
        (http(s?):)?([/|.|\w|\s])*\.(?:jpg|gif|png)
        I just changed one symbol, making the http(s?): optional rather than "or".

        Jared

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          never mind, I found a flaw, I put two img urls with a word in between, and it recognized it all as one phrase. Let me think just a moment, we need to disallow spaces...

          Comment

          • jhardman
            Recognized Expert Specialist
            • Jan 2007
            • 3405

            #6
            This worked in my tests:
            Code:
            (http(s)?://)?[.\w]{3,}.(jpg|gif|png)
            Last edited by jhardman; May 21 '10, 03:34 PM. Reason: allowed for https

            Comment

            • motaweb
              New Member
              • May 2010
              • 3

              #7
              Thanks a lot, you save me lot of time learning RegEx (but still, I need to learn it anyway...)

              Comment

              Working...