Convert regex expression to erlang re? Syntax

I have a hard time converting the following regular expression into erlang syntax.

I have a test line:

1,2 ==> 3 #SUP: 1 #CONF: 1.0

And the regex that I created with regex101 is (see below):

([\d,]+).*==>\s*(\d+)\s*#SUP:\s*(\d)\s*#CONF:\s*(\d+.\d+)

Regex:

But I get weird match results if I convert it to erlang - here is my attempt:

{ok, M} = re:compile("([\\d,]+).*==>\\s*(\\d+)\\s*#SUP:\\s*(\\d)\\s*#CONF:\\s*(\\d+.\\d+)").
re:run("1,2 ==> 3 #SUP: 1 #CONF: 1.0", M).

In addition, I get more than four matches. What am I doing wrong?

Here is the regex101 version: https://regex101.com/r/xJ9fP2/1

+3
source share
2 answers

I don’t know much about erlang, but I will try to explain. With your regular expression

>{ok, M} = re:compile("([\\d,]+).*==>\\s*(\\d+)\\s*#SUP:\\s*(\\d)\\s*#CONF:\\s*(\\d+.\\d+)").
>re:run("1,2 ==> 3 #SUP: 1 #CONF: 1.0", M).                                                  
{match,[{0, 28},{0,3},{8,1},{16,1},{25,3}]}
         ^^ ^^
         || ||
         || Total number of matched characters from starting index
   Starting index of match

Reason for more than four groups

, complete, - , . , 5 .

([\\d,]+).*==>\\s*(\\d+)\\s*#SUP:\\s*(\\d)\\s*#CONF:\\s*(\\d+.\\d+)
<------->         <---->             <--->              <--------->
First group    Second group       Third group           Fourth group
<----------------------------------------------------------------->
This regex matches entire string and is first match you are getting
                      (Zero'th group)

, ( ). all_but_first,

> re:run("1,2 ==> 3 #SUP: 1 #CONF: 1.0", M, [{capture, all_but_first, list}]).                
{match,["1,2","3","1","1.0"]}

+5

, :

1> RE = "([\\d,]+).*==>\\s*(\\d+)\\s*#SUP:\\s*(\\d)\\s*#CONF:\\s*(\\d+.\\d+)".
"([\\d,]+).*==>\\s*(\\d+)\\s*#SUP:\\s*(\\d)\\s*#CONF:\\s*(\\d+.\\d+)"
2> io:format("RE: /~s/~n", [RE]).
RE: /([\d,]+).*==>\s*(\d+)\s*#SUP:\s*(\d)\s*#CONF:\s*(\d+.\d+)/

rock321987.

0

All Articles