高分 求一条正则表达式(在线等)

kevin 2009-08-19 02:18:06
应用场景:
int i = 1;
if a = 1 and b = 1 and c = 1 then
return 0
else
return 1
end if;

替换成
int i = 1
if a == 1 and b == 1 and c == 1 :
return 0
else:
return 1


条件控制语句则进行替换,变量赋值不进行替换.
主要写出语句中 = 号的替换表达式

请问RegEx 的怎么写?



...全文
152 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DarkChampion 2009-08-21
  • 打赏
  • 举报
回复
现在可以了

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
DarkChampion 2009-08-21
  • 打赏
  • 举报
回复
reobj = re.compile(r'(?<!=)([^!><])=([^!><])(?!=)', re.VERBOSE)
result = reobj.sub(r'\1==\2',old)
print result


这样写不受空格影响,但是也会把赋值语句给改了。
kevin 2009-08-21
  • 打赏
  • 举报
回复
Perl 的看不懂啊。
我把其他帖子里的贴过来, 大家一起看看,怎么样可以完全解决这个问题。
RegEx:(?<!=)([^!><])=([^!><])(?!=)
Replace:\1\2==\4\5

这个方法还是存在问题:

1、变量赋值
2、如果 = 前后存在空格, 也会影响输出结果。
(?<=Exp)的用法可以参考下,大家再研究研究
DarkChampion 2009-08-20
  • 打赏
  • 举报
回复
学习一下,都是用Perl的?
fibbery 2009-08-20
  • 打赏
  • 举报
回复
你的程序中为什么使用全角分号?????

修改后的程序:
修改后的程序:
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");
}
fibbery 2009-08-20
  • 打赏
  • 举报
回复
我把要转换的内容保存在your.txt文件中,转换的结果在屏幕上输出。上面的程序有点问题,因为我忽略了转换前有分号,而转换后没有分号。下面再次修改程序。
fibbery 2009-08-20
  • 打赏
  • 举报
回复
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");
}


your.txt
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
gift_lbs 2009-08-20
  • 打赏
  • 举报
回复
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

删除最后的end if没有解决 没有时间了 开会去了
raistlin_t 2009-08-20
  • 打赏
  • 举报
回复
3楼是高手,学习了
guzl86 2009-08-19
  • 打赏
  • 举报
回复
学习
DarkChampion 2009-08-19
  • 打赏
  • 举报
回复
我对正则表达式实在是不熟

试着写了一个
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


结果把b给弄没了
if a == 1 and c == 1

哪个高手帮忙改改
Aylazhang 2009-08-19
  • 打赏
  • 举报
回复
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;
kevin 2009-08-19
  • 打赏
  • 举报
回复
应该不是太复杂吧, python的高手去哪里了?
nabice 2009-08-19
  • 打赏
  • 举报
回复
你这个太难了吧,我是来等分的。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧