[erlang-questions] How best to resolve apparent inconsistency between re:run and string:slice

Scott Finnie Scott.Finnie@REDACTED
Thu Aug 29 08:59:21 CEST 2019


Hello all



I discovered an apparent inconsistency between re:run/2 and string:slice/2 last night.  I'd appreciate any suggestions on the right way to solve.  Best illustrated by this example:



1> S1="\rDogs".

"\rDogs"

2> S2="\r\nDogs".

"\r\nDogs"

3> {match, [{_, To1}]}=re:run(S1, "^\r").

{match,[{0,1}]}

4> {match, [{_, To2}]}=re:run(S2, "^\r\n").

{match,[{0,2}]}

5> string:slice(S1,To1).

"Dogs"

6> string:slice(S2,To2).

"ogs"



I'd guess this is due to string:slice treating "\r\n" as a single lexeme, whereas re:run treats it as two characters.



What I'm trying to achieve is consistent behaviour that (a) recognises any newline (CR, LF, CRLF, ...) at the start of the string and removes it.  I also need a count of the contiguous newlines found.



As an aside, the now-obsolete string:sub_string/2 does seem to work consistently:



7> string:sub_string(S1,To1+1).

"Dogs"

8> string:sub_string(S2,To2+1).

"Dogs"



Thanks,

Scott.

________________________________

--------------------
Hymans Robertson LLP is a limited liability partnership registered in
England and Wales with registered number OC310282. A list of
members of Hymans Robertson LLP is available for inspection at One
London Wall, London, EC2Y 5EA, the firm's registered office. Hymans
Robertson LLP is authorised and regulated by the Financial Conduct
Authority and licensed by the Institute and Faculty of Actuaries for
a range of investment business activities.

This e-mail and any attachments are confidential. If it is not intended for
you then please tell us and respect that confidentiality. E-mails and
attachments can be corrupted or altered after sending: if you rely on
advice or product transmitted by e-mail then you do so at your own
risk. This footnote also confirms that this email message has been swept
for the presence of computer viruses.

Visit hymans.co.uk/information/privacy-notice/<http://www.hymans.co.uk/information/privacy-notice/> for
details of how we use your personal information.
--------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190829/d7351b52/attachment.htm>


More information about the erlang-questions mailing list