[erlang-questions] Backreferences with re module

Technion technion@REDACTED
Sun Apr 22 14:21:24 CEST 2018

Thanks a heap Steve. I must have read that man page a dozen times but that two liner escaped me every time.

From: vinoski@REDACTED <vinoski@REDACTED> on behalf of Steve Vinoski <vinoski@REDACTED>
Sent: Sunday, 22 April 2018 10:12:02 PM
To: Technion
Cc: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Backreferences with re module

On Sun, Apr 22, 2018 at 7:53 AM, Technion <technion@REDACTED<mailto:technion@REDACTED>> wrote:


I seem to be having some issues with the re: module any time a back reference is introduced.

(yes, I ideally wouldn't use a regex, I have one place I really need to)

Referencing the module manual here:


A specific example is given:

Consider, for example:
If the subject is "xyz123abc123", the match point is the fourth character.

However, I've simplified my code down to that exact example and I can't make it match:

46> {ok, MP} = re:compile("(.*)abc\1").

You need a double backslash here instead:

{ok, MP} = re:compile("(.*)abc\\1").

Look at the note near the top of the re man page http://erlang.org/doc/man/re.html :

'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, "\\".'


47> re:run("xyz123abc123", MP, [{capture,all_names,binary}]).

I have written all manner of patterns without a back reference that return a match fine:

48> {ok, MP2} = re:compile("(.*)abc123").
50> re:run("xyz123abc123", MP2, [{capture,all,binary}]).

My platform:

Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.3  (abort with ^G)

The presented syntax works perfectly fine in Ruby, further suggesting the regex is correct:

irb(main):001:0> /(.*)abc\1/.match("xyz123abc123")
=> #<MatchData "123abc123" 1:"123">

Any assistance on this is appreciated.

erlang-questions mailing list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180422/8be8a349/attachment.htm>

More information about the erlang-questions mailing list