日期:2014-05-20  浏览次数:20775 次

问个java正则表达式匹配字符串的问题,在线等。
一个字符串是这样的:s=“van Bakel,H., Nislow,C., Blencowe,B.J. and Hughes,T.R. Most ‘‘dark matter’’ transcripts are associated with known genes. PLoS Biol., 8, e1000371.”
我相匹配出这个字符串s1=“Most ‘‘dark matter’’ transcripts are associated with known genes”
s1的前后都不固定,“PLoS Biol”这是某个期刊的名称,开头是都是大写的,我想从s中匹配出s1,请问正则表达式该如何写?

------解决方案--------------------
(?<=T.R. \b).+\b(?=\bPLoS\b)
但是如果前后都不固定 怎么写我也不知道,零宽断言他总得有个对应的表达式啊。其实最简单的便是匹配你的s1,也就是“Most ‘‘dark matter’’ transcripts are associated with known genes” 自然就获得了这个字符串,但是我不明白你到底要做啥。
------解决方案--------------------
for example
Java code
String s="van Bakel,H., Nislow,C., Blencowe,B.J. and Hughes,T.R. Most ‘‘dark matter’’ transcripts are associated with known genes. PLoS Biol., 8, e1000371.";
Pattern p = Pattern.compile("(?<=[.]\\s)[A-Z].*?(?=[.]\\s[A-Z])"); //(.空格开始)+大写字母+任意字符+(.空格+大写字母结束)
Matcher m = p.matcher(s);
while (m.find()) {
    System.out.println(m.group());
}