3
$\begingroup$

I want the last number after the "/". How can I do that?

"http://arxiv.org/abs/math/0208009v1"

I just want 0208009v1

I want to import multiple links from the following:

{{"http://arxiv.org/abs/math/0208009v1"}, \
{"http://arxiv.org/abs/0905.0227v1"}, \
{"http://arxiv.org/abs/0907.5143v2"}, \
{"http://arxiv.org/abs/math/0509348v1"}, \
{"http://arxiv.org/abs/math/0608711v2"}, \
{"http://arxiv.org/abs/math-ph/0002018v2"}}

I just want the last number in the following links. How would that work?

$\endgroup$
6

2 Answers 2

4
$\begingroup$

In addition, using a modification of this regex:

(StringCases[#, RegularExpression["[^/]+$"]]&/@list)//Flatten

{0208009v1, 0905.0227v1, 0907.5143v2, 0509348v1, 0608711v2, 0002018v2}

where

list = {{"http://arxiv.org/abs/math/0208009v1"}, 
{"http://arxiv.org/abs/0905.0227v1"}, 
{"http://arxiv.org/abs/0907.5143v2"},
{"http://arxiv.org/abs/math/0509348v1"}, 
{"http://arxiv.org/abs/math/0608711v2"}, 
{"http://arxiv.org/abs/math-ph/0002018v2"}};
$\endgroup$
1
  • $\begingroup$ Maybe (StringCases[#, RegularExpression["([^/]+)/?$"]:> "$1"]&/@list)//Flatten if there is an unwanted trailing slash (eg "arxiv.org/abs/math-ph/0002018v2/") $\endgroup$
    – user1066
    Aug 8, 2020 at 4:48
3
$\begingroup$
list = {{"http://arxiv.org/abs/math/0208009v1"}, 
 {"http://arxiv.org/abs/0905.0227v1"}, 
 {"http://arxiv.org/abs/0907.5143v2"},
 {"http://arxiv.org/abs/math/0509348v1"}, 
 {"http://arxiv.org/abs/math/0608711v2"}, 
 {"http://arxiv.org/abs/math-ph/0002018v2"}};

In addition to StringSplit suggested by J.M. in comments,

StringSplit[Flatten@list, "/"][[All, -1]]

you can also use

StringTrim

StringTrim[Flatten[list], StartOfString ~~ ___ ~~ "/"]

StringReplace

StringReplace[Flatten[list], StartOfString ~~ ___ ~~ "/" -> ""]

StringCases

Flatten @ StringCases[Flatten@list, "/" ~~ a : Except["/"] .. ~~ EndOfString :> a]

StringDrop + StringPosition:

MapThread[StringDrop, {Flatten@list, Max /@ StringPosition[Flatten@list, "/"]}]

FileNameSplit

Last /@ FileNameSplit /@ Flatten[list]

to get

{"0208009v1", "0905.0227v1", "0907.5143v2", "0509348v1", "0608711v2",    "0002018v2"}
$\endgroup$
2
  • 3
    $\begingroup$ Consider also Last[URLParse[#, "Path"]] & /@ Flatten[list] $\endgroup$ Aug 7, 2020 at 13:13
  • 2
    $\begingroup$ I'm pretty sure the simplest solution is just StringExtract[Flatten[list], "/" -> -1]. $\endgroup$ Aug 8, 2020 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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