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
Bug in re.sub() #46101
Comments
Here is my source: Here is what I get: >>> trace.truc()
hi hello
helloyou
>>> |
In other words, if I understand correctly:
>>> re.sub('$', '#', 'a\nb\nc')
'a\nb\nc#'
>>> re.sub('$', '#', 'a\nb\n')
'a\nb#\n#' The first sample is correct, but the second one find two matches, even Is this normal? The docs say:
|
Fredrik? |
re.findall has the same behaviour. Without looking at the code, I'm not |
As re provides regular expression matching operations similar to those """ $a = 'a\nb\nc'; $a = 'a\nb\n'; $ perl pru_sub.pl
$VAR1 = 'a\\nb\\nc#';
$VAR1 = 'a\\nb\\n#'; |
Careful, Perl strings must be double-quoted for \n to be understood as """ $a = "a\nb\nc"; $a = "a\nb\n"; And the output is: $VAR1 = 'a Which is definitely different from python output. |
At least, the docs for re.M are consistent with the current behavior. |
So if the input ends in '\n', '$' matches both before and after that |
In the previous samples we forgot the /g option needed to match ALL """ $a = "a\nb\nc"; $a = "a\nb\n"; Which now gives the same output as Python: $VAR1 = 'a Perl is too difficult for us ;-) What shall we do?
|
Then I'd say, this is the correct semantics, for better or for worse; |
For the record, $ is defined to match "before a newline at the end of (and I have a vague memory that the "before a newline" behaviour in
Of course it does: re.sub scans the string for matches from left to |
Which is why I like to use \Z to match *only* the end of the string. |
Aha, I always thought that \Z was an alias for $. |
This may be a surprising behaviour, but consistent with Perl and the Committed as r59896. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: