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

求问一个分割方法. -- 把特定字符之间的字符剥离出来.
条件:
<img src=\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\"><img src=\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\">
分离后想得到的结果:
String[] sArray={"jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\","jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\"};

求思路, 源码, 或者高效方法.

------解决方案--------------------
注意正则里有贪婪与非贪婪模式: +与+?
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Hello {
    public static void main(String[] args) throws Exception {
        String str =
                "<img src=\\\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\\\"><img src=\\\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\\\">"
        ;

        String patternString = "<img src=\\\\\"data:image/(.+?)\">";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(str);

        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}

输出
jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\
jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAOijjEUYRScD1ooooA//Z\

------解决方案--------------------
正则真是个神奇的东西,总是会把俺绕晕。。
------解决方案--------------------