This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re.sub doesn't handle properly matches with yes-pattern no-pattern
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, oliv
Priority: normal Keywords:

Created on 2010-03-12 10:59 by oliv, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg100926 - (view) Author: (oliv) Date: 2010-03-12 10:59
Using python 2.6.4 on Arch Linux

The regular expression syntax in python indicate: 

(?(id/name)yes-pattern|no-pattern)
Will try to match with yes-pattern  if the group with given id or name exists, and with no-pattern  if it doesn’t.

I used that functionnality but the code doesn't works anymore:
import re
myre = re.compile('(?P<port>[0-9]+/(tcp|udp))\s+(?P<status>\S+)\s+(?P<service>[a-zA-Z0-9_-]+)\s*(?P<details>.*)')
myre.sub('(?(<details>)\g<details>|\g<service>)',"443/tcp open  ssl      Microsoft IIS SSL")

> The output is:
'(?(<details>)Microsoft IIS SSL|ssl)'
Instead of:
Microsoft IIS SSL

It looks like a bug as it was working earlier...
msg100937 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2010-03-12 15:35
You can't use (...) syntax in substitution strings, only \N references.
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52369
2010-03-12 15:35:57gvanrossumsetstatus: open -> closed

nosy: + gvanrossum
messages: + msg100937

resolution: not a bug
2010-03-12 15:04:43brian.curtinsetpriority: normal
type: behavior
components: + Library (Lib), - Build
stage: test needed
2010-03-12 10:59:16olivcreate