[issue43714] re.split(), re.sub(): '\Z' must consume end of string if it matched

Alexander Grigoriev report at bugs.python.org
Sat Apr 3 22:35:59 EDT 2021


Alexander Grigoriev <alegrigoriev at gmail.com> added the comment:

For example, sed:

$ sed --version
sed (GNU sed) 4.8
Copyright (C) 2020 Free Software Foundation, Inc.

$ sed -e 's/-\?$/x/g' <<<'a-b-'
a-bx

Perl:
$ perl --version

This is perl 5, version 32, subversion 0 (v5.32.0) built for x86_64-msys-thread-multi

Copyright 1987-2020, Larry Wall
$ perl -e 'my $x="a-b-"; $x =~ s/-?$/x/g; print $x'
a-bxx

https://www.freeformatter.com/java-regex-tester.html

Java Regular Expression :
-?$
Entry to test against :
a-b-c-
String replacement result:
a-b-cx

During replacement or split, a match consumes the matched character. It's easy to forget that "end of line" should be considered a (pseudo)character and must also be consumed if it matched.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43714>
_______________________________________


More information about the Python-bugs-list mailing list