Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match IP address with regexp in Erlang

Tags:

regex

erlang

I'm studying the re module of Erlang, and I just want to match an IP address in a URL:

Url = "http://192.168.1.241/mod/fun?arg",
re:run(Url, "(\\d{1,3}\\.){3}\\d{1,3}", [{capture, all, list}]).

But it returned {match,["192.168.1.168","1."]} to me. Why is "1." in the return list?

like image 464
lujb Avatar asked Jan 20 '23 21:01

lujb


1 Answers

You've specified "all" for the ValueSpec, which means you'll get all matching subgroups. In this case that includes "1.". Instead of "all" you can just specify "first" and all you'll get is the first matching group (the complete IP).

You should do it like this:

Url = "http://192.168.1.241/mod/fun?arg",
re:run(Url, "(\\d{1,3}\\.){3}\\d{1,3}", [{capture, first, list}]).

This will return:

{match,["192.168.1.241"]}

More info here.

EDIT: Just in case you miss it, here's the relevant part in the docs (which explain it a lot better than me :-)):

Specifies which captured (sub)patterns are to be returned. The ValueSpec can either be an atom describing a predefined set of return values, or a list containing either the indexes or the names of specific subpatterns to return.

The predefined sets of subpatterns are:

all

All captured subpatterns including the complete matching string. This is the default.

first

Only the first captured subpattern, which is always the complete matching part of the subject. All explicitly captured subpatterns are discarded.

all_but_first

All but the first matching subpattern, i.e. all explicitly captured subpatterns, but not the complete matching part of the subject string. This is useful if the regular expression as a whole matches a large part of the subject, but the part you're interested in is in an explicitly captured subpattern. If the return type is list or binary, not returning subpatterns you're not interested in is a good way to optimize.

none

Do not return matching subpatterns at all, yielding the single atom match as the return value of the function when matching successfully instead of the {match, list()} return. Specifying an empty list gives the same behavior.

like image 60
João Neves Avatar answered Jan 28 '23 13:01

João Neves