#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
就是我想把代码文件中所有的 #ifdef MODIFY ..... #endif //MODIFY' 的内容用正则表达式提取出来,然后存放在一个列表中结果如下: 最终要获取这样一个列表。请问正则表达式怎么写,尝试了好久也没弄出满意的,请大家帮我解决啊,给高分
[ '#ifdef MODIFY \ncode1\n#else //MODIFY\ncode2\n#endif //MODIFY', '#ifdef MODIFY \ncode6\n#else //MODIFY\ncode7\n#endif //MODIFY']
5 个解决方案
#1
patternStr = '#ifdef MODIFY[\s\S]*#endif \/\/MODIFY'
pattern = re.compile(patternStr)
match = pattern.findall(sample)
match应该就是你要的的那个list
#2
修改了一下,应该使用非贪婪模式
#3
import re
sample = '''
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
'''
pat = re.compile(r'(#ifdef\s*MODIFY.*?#endif\s*//MODIFY)', re.I|re.S|re.M)
m = pat.findall(sample)
for x in m:
print '-' * 32
print x
#4
非常感谢二位大侠的解答。
#5
perl code
cat test.pl
#!/usr/bin/perl -w
use strict;
undef $/;
my $line = <>;
my @lines = $line =~ m{(^#ifdef\s+MODIFY.+?^#endif\s+//MODIFY)}msg;
for (@lines){
print "####################\n";
print "$_\n";
}
cat linux.txt
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
test.pl linux.txt
####################
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
####################
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
cat test.pl
#!/usr/bin/perl -w
use strict;
undef $/;
my $line = <>;
my @lines = $line =~ m{(^#ifdef\s+MODIFY.+?^#endif\s+//MODIFY)}msg;
for (@lines){
print "####################\n";
print "$_\n";
}
cat linux.txt
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
test.pl linux.txt
####################
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
####################
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
#1
patternStr = '#ifdef MODIFY[\s\S]*#endif \/\/MODIFY'
pattern = re.compile(patternStr)
match = pattern.findall(sample)
match应该就是你要的的那个list
#2
修改了一下,应该使用非贪婪模式
#3
import re
sample = '''
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
'''
pat = re.compile(r'(#ifdef\s*MODIFY.*?#endif\s*//MODIFY)', re.I|re.S|re.M)
m = pat.findall(sample)
for x in m:
print '-' * 32
print x
#4
非常感谢二位大侠的解答。
#5
perl code
cat test.pl
#!/usr/bin/perl -w
use strict;
undef $/;
my $line = <>;
my @lines = $line =~ m{(^#ifdef\s+MODIFY.+?^#endif\s+//MODIFY)}msg;
for (@lines){
print "####################\n";
print "$_\n";
}
cat linux.txt
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
test.pl linux.txt
####################
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
####################
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
cat test.pl
#!/usr/bin/perl -w
use strict;
undef $/;
my $line = <>;
my @lines = $line =~ m{(^#ifdef\s+MODIFY.+?^#endif\s+//MODIFY)}msg;
for (@lines){
print "####################\n";
print "$_\n";
}
cat linux.txt
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
code3\n
code4\n
code5\n
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY
test.pl linux.txt
####################
#ifdef MODIFY \n
code1\n
#else //MODIFY\n
code2\n
#endif //MODIFY
####################
#ifdef MODIFY \n
code6\n
#else //MODIFY\n
code7\n
#endif //MODIFY