Autonomía digital y tecnológica

Código e ideas para una internet distribuida

Linkoteca. expresiones regulares


Python is a high level open source scripting language. Python’s built-in «re» module provides excellent support for regular expressions, with a modern and complete regex flavor. The only significant features missing from Python’s regex syntax are atomic grouping, possessive quantifiers, and Unicode properties.

The first thing to do is to import the regexp module into your script with import re.

The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

When braces are used, the matching ending brace is the first ‘}’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

The :global command is your friend – learn it well. It lets you run arbitrary :ex commands on every line that matches a regex. It abbreviates to :g.

To delete all lines that match «George Bush»:

:g/George Bush/ d

The command that follows can have its own address/range prefix, which will be relative to the matched line. So to delete the 5th line after George Bush:

:g/George Bush/ .+5 d

To delete the DEBUG log entries:

:g/DEBUG/ .,+10 d