Is it possible to do something like the following Perl code in Python? From what I can tell the answer is no, but I figured I'd double check.
The Perl code I want to replicate in Python:
#!/usr/bin/perl
my $line = "hello1234world";
if($line=~/hello(.*)world/) {
print($1);
}
#prints 1234
The following is the closest stylistically I can think off, but when running I (obviously) get the following error:
import re
line = "hello1234world"
if matchObj = re.match(r'hello(.*)world',line):
print(matchObj.group(1))
#error: if matchObj = re.match(r'hello(.*)world',line):
#error: ^
#error: SyntaxError: invalid syntax
The following is the best working code I can come up with:
import re
line = "hello1234world"
matchObj = re.match(r'hello(.*)world',line)
if matchObj:
print(matchObj.group(1))
#prints 1234
I'd really like to avoid a separate line for the variable declaration and if statement if possible.