Tuesday 15 February 2011

regex - Pattern only matches once, when multiple matches are in the string -


I am trying to match url URLs that are attached to url tags

  [URL = http: //www.google.com/sdaasd/sadasda/asddsa/sadsa/dasd.html] [IMG] http://www.cnn.com/asd.jpg [/ IMG] [/ URL]   

I have the following pattern that works perfectly when matching with an example of one URL / IMG combo

  \ [URL = ("|) ([\ s \ s] *) (" | "] \ [img \] (https: \ / \ /.* \ (?: Png | jpg?)?) \ [\ / Img] \ [\ / url]]   

However, if I repeat url / imam combo:

  [URL = http: //www.google.com/sdaasd/ Sadasda / asddsa / sadsa / dasd.html] [Img] http://www.cnn.com/asd.jpg [/ img] [/ url] [url = http: //www.google.com/sdaasd/sadasda/asddsa/sadsa/dasd.html] [ IMG] http://www.cnn.com/asd.jpg [/ IMG] [/ URL]   

Then it does not work any ideas on a working / fix? ?

My guess is that you have to modify . * to use, . *? , like this:

  \ [URL = ("|) ([\ s \ S] *?) (" |)] \ [Img \] (https? : \ / \ /.*? \. (?: Png | jpg)) \ [\ / img] \ [\ url]]    

No comments:

Post a Comment