-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Regex.split with trailing \K works incorrectly #14467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Comments
You can directly use :re.split till the fix is ready
…On Sat, May 3, 2025 at 5:41 PM error256 ***@***.***> wrote:
*error256* created an issue (elixir-lang/elixir#14467)
<#14467>
Elixir 1.18.3, Erlang/OTP 27.3.3
IO.inspect Regex.split ~r/b\K/, "ababab"IO.inspect :re.split "ababab", "b\\K"
returns
["ab", "ab", "", "ab", "", ""]
["ab", "ab", "ab", ""]
I think Regex.split should return the same result as :re.split.
Non-trailing \Ks and lookbehinds seem to work correctly, this is the only
case.
—
Reply to this email directly, view it on GitHub
<#14467>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGJSGFKLY7JQHXCFIKTQCYD24UZZLAVCNFSM6AAAAAB4MEOPCWVHI2DSMVQWIX3LMV43ASLTON2WKOZTGAZTONRSGY2TONQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Here is the relevant Erlang code that deals with this and we are missing, in particular the first branch of the case: do_split(Subj, Offset, [[{MainI,MainL}|Sub]|T], NumSub, Limit, Group) ->
NewOffset = MainI+MainL,
KeptLen = MainI - Offset,
case {KeptLen,empty_sub(Sub),MainL} of
{0,true,0} ->
do_split(Subj,NewOffset,T,NumSub,Limit,Group);
_ ->
<<_:Offset/binary,Keep:KeptLen/binary,_/binary>> = Subj,
ESub = extend_subpatterns(Sub,NumSub),
Tail = do_split(Subj, NewOffset, T, NumSub, Limit - 1,Group),
case Group of
false ->
[Keep | dig_subpatterns(Subj,lists:reverse(ESub),Tail)];
true ->
[[Keep | dig_subpatterns(Subj,lists:reverse(ESub),[])]|
Tail]
end
end.
empty_sub([]) ->
true;
empty_sub([{_,0}|T]) ->
empty_sub(T);
empty_sub(_) ->
false. |
@josevalim I came to the same conclusion and tried a fix, but it breaks some tests so it might be a breaking change? iex> :re.split "abc", ""
["a", "b", "c", ""]
# main
iex> Regex.split ~r//, "abc"
["", "a", "b", "c", ""]
# the fix
iex> Regex.split ~r//, "abc"
["a", "b", "c", ""] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elixir 1.18.3, Erlang/OTP 27.3.3
returns
I think
Regex.split
should return the same result as:re.split
. Non-trailing\K
s and lookbehinds seem to work correctly, this is the only case.The text was updated successfully, but these errors were encountered: