7
$\begingroup$

If I have a list like

list = {"my element 1 http://link.com", "http://link2.com my element 2"}

how do I get a list of the links inside each element?

Something like

StringCases[list, "http" ~~ _]

probably but I wasn't able to know how to end my pattern.

$\endgroup$
  • $\begingroup$ Yet another way, Select[StringSplit[#], StringMatchQ[#, "http://*"] &] & /@ list. Sorry about the nested Function. $\endgroup$ – Szabolcs Oct 22 '13 at 15:01
  • $\begingroup$ @Szabolcs go answer! $\endgroup$ – Yves Klett Oct 22 '13 at 17:21
9
$\begingroup$

One way:

StringCases[list, "http://" ~~ Except[WhitespaceCharacter] ..]

{{"http://link.com"}, {"http://link2.com"}}

$\endgroup$
  • $\begingroup$ that's great but I don't see what the pattern ".." does in the documentation? $\endgroup$ – Sulli Oct 21 '13 at 12:22
  • 1
    $\begingroup$ @su1 Look up Repeated, a good friend on many a jolly pattern matching occasion :-) $\endgroup$ – Yves Klett Oct 21 '13 at 12:24
5
$\begingroup$

Another way, similar to @YvesKlett, but with RegularExpression.

StringCases[list, RegularExpression["http://\\S+"]]

{{"http://link.com"}, {"http://link2.com"}}

In RegularsExpression \\S means non space characters. You can find more information in Wolfram documentations here

$\endgroup$
  • 1
    $\begingroup$ You could give a short description of your regex to make it even more educational ;-) $\endgroup$ – Yves Klett Oct 22 '13 at 14:06
  • $\begingroup$ @YvesKlett Agree. Done! $\endgroup$ – Murta Oct 22 '13 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.