python / cpython

The Python programming language
https://www.python.org
Other
63.5k stars 30.41k forks source link

Introduce a method to concatenate regex patterns #77839

Open e2a53e08-d9ff-4ce2-9a94-6b72883af0fc opened 6 years ago

e2a53e08-d9ff-4ce2-9a94-6b72883af0fc commented 6 years ago
BPO 33658
Nosy @ezio-melotti, @serhiy-storchaka

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: ```python assignee = None closed_at = None created_at = labels = ['expert-regex', 'type-feature', '3.7'] title = 'Introduce a method to concatenate regex patterns' updated_at = user = 'https://bugs.python.org/aleskva' ``` bugs.python.org fields: ```python activity = actor = 'serhiy.storchaka' assignee = 'none' closed = False closed_date = None closer = None components = ['Regular Expressions'] creation = creator = 'aleskva' dependencies = [] files = [] hgrepos = [] issue_num = 33658 keywords = [] message_count = 2.0 messages = ['317787', '317829'] nosy_count = 5.0 nosy_names = ['ezio.melotti', 'mrabarnett', 'THRlWiTi', 'serhiy.storchaka', 'aleskva'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue33658' versions = ['Python 3.7'] ```

e2a53e08-d9ff-4ce2-9a94-6b72883af0fc commented 6 years ago

After removing inline flags (deprecated in https://bugs.python.org/issue22493) piping patterns will not work: pats = [r'(?m)^line.continues$', r'(?s)begin.*?end'] re.compile('|'.join(pats))

Maybe there should be introduced some method to merge patterns (similar to re.escape)?

serhiy-storchaka commented 6 years ago

This is not trivial task because inline flags with global scope can be occurred in any part of the pattern (although using them not at the start of the pattern is deprecated).

But starting with 3.7 you can use inline flags with local scope. This will allow you to use simple string concatenation.

pats = [r'(?m:^line.continues$)', r'(?s:begin.*?end)']