Reputation: 4721
I am trying to translate a few directives for regular expressions from Perl to Python.
I am specifically looking for being able to translate the following three directives:
while ($string =~ s/A/B/g) {
}
$string =~ s/A/B/g;
if ($string =~ m/a/)
{
## do something
}
It seems like you can't use the usual s// and m// syntax that Perl and other unix tools have. Is it really true, and I would have to look deeply into regexp analysis in Python, or is there some easy way to translate these directives?
Upvotes: 0
Views: 202
Reputation: 4817
Have a look at the re
module. With re
, you can do things like this:
import re
pattern = re.compile(r'[A-Z]')
if pattern.search(string):
print "string contains a capital letter"
You can also do substitutions using the sub
method instead of search
.
Upvotes: 3