java如何截取固定字符间的字符串?
我有一个txt格式的文档,假如是这样的:
————————————————————————————————————————
NULL, /* link_id = 5 */ \
NULL, /* link_id = 6 */ \
char*suit_case_am_num_01(void);
NULL, /* link_id = 12 */ \
NULL, /* link_id = 13 */ \
NULL, /* link_id = 14 */ \
NULL /* link_id = 15 */ \
char*suit_case_am_num_02(void);
char*suit_case_am_num_03(void);
NULL, /* link_id = 12 */ \
NULL, /* link_id = 13 */ \
NULL, /* link_id = 14 */ \
NULL /* link_id = 15 */ \
char*suit_case_am_num_04(void);
char*suit_case_am_num_05(void);
————————————————————————————————————————
我想将所有以char*开头,(void)结尾的中间那一段字符串截取出来:
最终希望得到的是这样的一串:
suit_case_am_num_01
suit_case_am_num_02
suit_case_am_num_03
suit_case_am_num_04
suit_case_am_num_05
改怎么写这段代码?
------解决方案--------------------
我就会笨办法,仅供参考。
Java code
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
public class FindStringWithChar
{
public static void main(String[] args)
{
try
{
BufferedReader bf=new BufferedReader(new FileReader("3.txt"));
String str=null;
while((str=bf.readLine())!=null)
{
Pattern pattern = Pattern.compile("^char.*"); //找到char开头的字符串。
Matcher matcher = pattern.matcher(str);
while (matcher.find())
{
String s=matcher.group().replaceAll("char\\*","").replaceAll("\\(void\\);",""); //替换掉头"char*"和尾"(void);.
System.out.println(s);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}