我有这样的字符串
s = '''int t; //variable t
t->a=0; //t->a does;; something
printf("\nEnter the Employee ID : ");
scanf("%d", ptrx->eid); //employee id ptrx->eid
printf("\nEnter the Employee Name : ");
scanf("%s", ptr->name);
return 0;'''
我想用上面的字符串替换->它。.但是这种替换不应该在评论中完成。注释是一个以行开头//并在行末结束的字符串。
我试过下面的代码。有什么办法可以使用单个正则表达式解决这个问题。
代码
import re
for line in s.split('\n'):
code = re.findall('^(?:(?!\/\/.+$).)*', line)
comment = re.findall('\/\/.+$', line)
print(''.join(code).replace('->', '.') + ''.join(comment))
预期输出:
int t; //variable t
t.a=0; //t->a does;; something
printf("
Enter the Employee ID : ");
scanf("%d", ptrx.eid); //employee id ptrx->eid
printf("
Enter the Employee Name : ");
scanf("%s", ptr.name);
return 0;
一只名叫tom的猫
相关分类