I have a specification of the command
RENAME_SECTION file::section [new_file::]new_section
file, section and new_section are mandatory, new_file is optional.
Than means that all next expressions will match:
RENAME_SECTION io.cfg::BUS1 io.cfg::BUS3
RENAME_SECTION io.cfg::BUS2 io_new.cfg::BUS2
RENAME_SECTION io.cfg::VID VID1
I try to get all provided parameters with Python re.search(pattern, config_line) using the pattern
(?P<command>RENAME_SECTION\s)\s*(?P<file>\S+)::(?P<section>.*)\s+(?P<target_file>\S*)[::]?(?P<target_section>.*)
This pattern match two first cases, but not the third one, the reason is absents of last ::.
How can I bundle :: with target_file named capture group?
You may try this regex with an optional match and anchors:
^(?P<command>RENAME_SECTION)\s+(?P<file>\S+?)::(?P<section>\S+)\s+(?:(?P<target_file>\S+?)::)?(?P<target_section>.+)$
RegEx Demo
(?:(?P<target_file>\S+?)::)? is an optional non-capturing group that makes matching target_file and trailing :: optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With