在JAVA中怎么只匹配文件中的以begin开始,以end 结束的一段内容?因为这个文件比较大,但是我需要的内容仅是文件中一小部分,我想把文件指针定位在begin和end 之间,然后用正则表达式按行匹配并输出.文件内容大概如下:
begin
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
endbegin
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
endbegin
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
end
begin
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
endbegin
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
endbegin
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
end
如果这个大文件是你自己生成的,建议改变生成方式,或者增加相关的描述性文件。如果不是你生成的,那就杯具了
3楼的意思应该一行一行读了之后,先匹配begin啊,匹配到了begin之后再寻找你需要的内容。
xxxxxxxxxxxxxxx
begin
xxxxxxxxxxxxxxxx
end
xxxxxxxxxxxxxxxxx
end
xxxxxxxxxxxxxxxxx
你是要读vb的代码吧?要读方法体么?
如果是要读方法体的话,仅以begin开头,以end结尾是不够的。begin-end有时候也作为流程控制语句的开始和结束的。所以你要想办法将这个begin定位到function definition之后。然后让其后所有的begin-end配对。在找到与第一个begin匹配的end的时候再结束。