Using Python I need to delete all characters in a multiline string up to the first occurrence of a given pattern. In Perl this can be done using regular expressions with something like:
#remove all chars up to first occurrence of cat or dog or rat
$pattern = 'cat|dog|rat'
$pagetext =~ s/(.*?)($pattern)/$2/xms;
What’s the best way to do it in Python?
You want to delete all characters preceding the first occurrence of a pattern; as an example, you give “cat|dog|rat”.
Code that achieves this using
re:or, if you’ll be using again this regular expression:
Note the non-greedy
.*?. The initial(?s)allows to match newline characters too, before the word matching.Examples:
In case you want to do the conversion only for the words cat, dog and rat, you’ll have to change the regex into: