3

This is the text sample:

$text = "asd dasjfd fdsfsd http://11111.com/asdasd/?s=423%423%2F gfsdf http://22222.com/asdasd/?s=423%423%2F
asdfggasd http://3333333.com/asdasd/?s=423%423%2F";

This is my regex pattern:

preg_match_all( "#http:\/\/(.*?)[\s|\n]#is", $text, $m );

That match the first two urls, but how do I match the last one? I tried adding [\s|\n|$] but that will also only match the first two urls.

James Harzs
  • 1,853
  • 5
  • 21
  • 30

3 Answers3

4

Don't try to match \n (there's no line break after all!) and instead use $ (which will match to the end of the string).

Edit: I'd love to hear why my initial idea doesn't work, so in case you know it, let me know. I'd guess because [] tries to match one character, while end of line isn't one? :)

This one will work:

preg_match_all('#http://(\S+)#is', $text, $m);

Note that you don't have to escape the / due to them not being the delimiting character, but you'd have to escape the \ as you're using double quotes (so the string is parsed). Instead I used single quotes for this.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • "#http:\/\/(.*?)[\s|\n|$]#is" will not match the 3rd url for me. – James Harzs Jun 17 '12 at 18:25
  • Interesting, trying it now as well. It indeed doesn't match (also; as mentioned above; the | is obsolete (and interpreted as another possible value within `[]`). – Mario Jun 17 '12 at 18:32
  • Within the square brackets, `$` matches a literal `$`, just like `|` matches a literal `|`. You would need to look for `(\s|$)`, as @dsrekab suggested (`\n` is redundant, as that's one of the characters matched by `\s`). But I think the way you're doing it now is better. – Alan Moore Jun 18 '12 at 01:16
0

I'm not familar with PHP, so I don't have the exact syntax, but maybe this will give you something to try. the [] means a character class so |$ will literally look for a $. I think what you'll need is another look ahead so something like this:

#http:\/\/(.*)(?=(\s|$))

I apologize if this is way off, but maybe it will give you another angle to try.

dsrekab
  • 456
  • 4
  • 13
0

See What is the best regular expression to check if a string is a valid URL?

It has some very long regular expressions that will match all urls.

Community
  • 1
  • 1
jpiasetz
  • 1,692
  • 4
  • 21
  • 35