Regular expression to delete multiple lines matching query


#1

This will be a water divider ;-)

Best Regards,
Fábio Sbano


#2

It seems like we could do this as a really thin wrapper around the Regex module:

https://docs.python.org/3/howto/regex.html

FileReplace(filename, in=pattern, out=pattern)

Where 'in', is a string or a regex, and 'out' could really be anything, including the function mentioned in the docs.


#3

Opened a ticket to track this - in case you want to try adding this I would be totally glad to help with questions, should be an exceptionally easy module to do.


#4

Yeah

Use it (BlockIn):

Ex: httpd.conf

<Directory />((\n)|(\s)(.+)){1,}</Directory>

Instead of

263     marker0 = re.sub(b(r'{mark}'), b('BEGIN'), marker)
264     marker1 = re.sub(b(r'{mark}'), b('END'), marker)
265     if present and block:
266         # Escape seqeuences like '\n' need to be handled in Ansible 1.x
267         if module.ansible_version.startswith('1.'):
268             block = re.sub('', block, '')
269         blocklines = [marker0] + block.splitlines() + [marker1]
270     else:
271         blocklines = []

Best Regards,
Fábio Sbano


#5

Yeah I'm not even sure how that old lineinfile module works anymore, I know it was a source of many dozen IRC and email list questions, so I'm looking to avoid as much of that confusion as possible.

if it is just a thin layer over Python's regex capabilities that seems easy enough.

I always do recommend templates and "settings.d" directories whenever I can, they are always easier to work with... but I recognize that if you are securing someone else's configuration or uncertain what the exact contents of a config file might be because there is another customer coming along to edit the file later, it's sometimes hard to do that.