在两个字符串之间查找字符

时间:2014-05-22 09:01:27

标签: java

下面给出了样本内容,我需要得到" body {"和"}"。我试过模式匹配器,但没有找到。有人可以帮我这个。

body {
font-family: Arial, Helvetica, sans-serif;
font-style: normal;
font-size: 10pt
color: #000000;
}

以下是代码:

String patt1 = "body {";
String patt2 = "}";
Pattern p1 = Pattern.compile(Pattern.quote(patt1) + "(.*?)" + Pattern.quote(patt2));
Matcher m1 = p1.matcher(cssContent);
while (m1.find())
 {
   System.out.println("Mathced string : "+m1.group(1));
 }

1 个答案:

答案 0 :(得分:0)

为它创建Regex模式:

   String hello = "body {font-family: Arial, Helvetica, sans-serif; font-style: normal; font-size: 10pt color: #000000; }";
   Pattern pattern = Pattern.compile("\\{(.+?)\\}", Pattern.DOTALL | Pattern.MULTILINE);
   Matcher matcher = pattern.matcher(hello);
   if (matcher.find()) {
        System.out.println(matcher.group(1));
    }