I have a working regex under vim: /^ \\{-}\\a.*$\\n
I implement a global search and replace as :%s/^ \\{-}\\a.*$\\n//
This works great -- removes all lines that start with any number of spaces (matched non-greedily), followed by a letter and anything else to the end of the line including the newline.
I cannot (to save my soul) figure out the analogous regex in Python. Here's what make sense to me:
x = re.sub("^ *?\a.$\n","",y)
But this doesn't do anything.
Many thanks for your sagacious replies.
\\a
means the bell character (0x07) in Python, and $\\n
is a redundant bad idea, so:
x = re.sub(r"^ *[A-Za-z].*\n","",y)
Also, there's no reason to write ' *?'
instead of ' *'
here, as it's always going to be followed by a non-space if it's matching.
If you want to match any number of whitespace, you can also use the \\s
sequence.
Any letter will be matched by the [a-zA-Z]
character class. You also don't need to use the $
and the \\n
, either will do.
Suggest the following:
x = re.sub(r"^\s*[a-zA-Z].*(\r|\n)","",y)
If you want at least one whitespace, use \\s+
instead of \\s*
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.