37,743
社区成员




import re
old = '''int i = 1
if a = 1 and b = 1 or c = 1 :
return 0
else
return 1
end if; '''
reobj = re.compile(r'\b(if|and|or)\b(\s*)(\w+\s*)=(\s*\d+)', re.VERBOSE)
result = reobj.sub(r'\1\2\3==\4',old)
print result
reobj = re.compile(r'(?<!=)([^!><])=([^!><])(?!=)', re.VERBOSE)
result = reobj.sub(r'\1==\2',old)
print result
use strict;
use warnings;
use IO::File;
my $f=new IO::File("< your.txt");
my $line;
while($line=<$f>)
{
chomp($line);
if($line=~/^if(.+)then/)
{
my $condition=$1;
$condition=~s/=/==/g;
$line="if $condition :";
}
$line=~s/end\s+if\s*;|;|;//i;
print("$line\n");
}
use strict;
use warnings;
use IO::File;
my $f=new IO::File("< your.txt");
my $line;
while($line=<$f>)
{
chomp($line);
if($line=~/^if(.+)then/)
{
my $condition=$1;
$condition=~s/=/==/g;
$line="if $condition :";
}
$line=~s/end\s+if\s*;//i;
print("$line\n");
}
int i = 1;
if a = 1 and b = 1 and c = 1 then
return 0
else
return 1
end if;
E:\MyDocs\debug\perl>perl test.pl
int i = 1;
if a == 1 and b == 1 and c == 1 :
return 0
else
return 1
import re
old = '''int i = 1
if a = 1 and b = 1 and c = 1 then
return 0
else
return 1
end if; '''
mid_1 = re.sub(r'=', '==', old)
mid_2 = re.sub(r'then', ':', mid)
result = re.sub(r'==', '=', mid_2, 1)
print result
import re
old = '''int i = 1
if a = 1 and b = 1 and c = 1 :
return 0
else
return 1
end if; '''
reobj = re.compile(r'(\bif\b\s+)((\w+\s*)=(\s*\d+))((\s+\w+\s+)((\w+\s*)=(\s*\d+)))*', re.VERBOSE)
result = reobj.sub(r'\1\3==\4\6\8==\9',old)
print result
use warnings;
use strict;
my @str = map {
s/;$//g;
s/=/==/g if s/(?=if)(.*)\s+then$/$1:/g;
s/^end if;$//ig;
$_;
} <DATA>;
print @str;
__DATA__
int i = 1;
if a = 1 and b = 1 and c = 1 then
return 0
else
return 1
end if;