Reputation: 409
I am running into some problems with extracting values from a url.
Here is my tested regex code: https://regex101.com/r/rfRmhh/1
In that code are backslashes, and I acted in accord to Erlang's note here by backslashing it again:
The Erlang literal syntax for strings uses the "\" (backslash) character as an escape code. You need to escape backslashes in literal strings, both in your code and in the shell, with an extra backslash, that is, "\". - http://erlang.org/doc/man/re.html
This is my code:
get_from_url()->
Pattern = "/(.+)-([0-9_]+)x([0-9_]+)(-[0-9a-zA-Z(),\\-._]+)*\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$/",
Url = "http://localhost:8001/78326459041381-200x100.jpg",
re:run(Url, Pattern).
It returns simply "nomatch". (But the regex works on the test site.) Secondly, even when I get a match it would return something like {match,[{0,14},{0,13}]}
. I assume these are the offsets of the matched variable in the list?
Would one then use the sublist(List1, Start, Len)
function to get the values?
Upvotes: 1
Views: 154
Reputation: 222088
You don't need the leading or trailing /
in the regex pattern. They are treated as a literal /
in Erlang (unlike PHP and probably more languages) which is why your match failed.
1> Pattern = "(.+)-([0-9_]+)x([0-9_]+)(-[0-9a-zA-Z(),\\-._]+)*\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$".
2> Url = "http://localhost:8001/78326459041381-200x100.jpg".
3> re:run(Url, Pattern).
{match,[{0,48},{0,36},{37,3},{41,3},{-1,0},{45,3}]}
This will return a {match, List}
where List
is a list of tuples containing the start offset and the length of the match. To get the captured values as a string or binary, you can use the capture
option:
4> re:run(Url, Pattern, [{capture, all, list}]).
{match,["http://localhost:8001/78326459041381-200x100.jpg",
"http://localhost:8001/78326459041381","200","100",[],
"jpg"]}
5> re:run(Url, Pattern, [{capture, all, binary}]).
{match,[<<"http://localhost:8001/78326459041381-200x100.jpg">>,
<<"http://localhost:8001/78326459041381">>,<<"200">>,
<<"100">>,<<>>,<<"jpg">>]}
Upvotes: 3