代码之家  ›  专栏  ›  技术社区  ›  giri

如何编写包含双引号(“?”)的Java字符串文字?

  •  0
  • giri  · 技术社区  · 16 年前

    我得到编译时错误。

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class gfile
     {
      public static void main(String args[]) {
        // create a Pattern
        Pattern p = Pattern.compile("<div class="dinner">(.*?)</div>");//some prob with this line
    
    
        // create a Matcher and use the Matcher.group() method
      String can="<tr>"+
                              "<td class="summaryinfo">"+
    
                                    "<div class="dinner">1,000</div>" +
                                    "<div style="margin-top:5px " +
                                     "font-weight:bold">times</div>"+
                                "</td>"+
                            "</tr>";
    
        Matcher matcher = p.matcher(can);
        // extract the group
    
        if(matcher.find())
         {
        System.out.println(matcher.group());
         }
      else
         System.out.println("could not find");
      }
    }
    
    5 回复  |  直到 14 年前
        1
  •  7
  •   Eric G    16 年前

    在对pattern.compile的调用中有未转换的引号。

    变化:

    Pattern p = Pattern.compile("<div class="dinner">(.*?)</div>");
    

    到:

    Pattern p = Pattern.compile("<div class=\"dinner\">(.*?)</div>");
    

    注意:我刚在你的线罐里看到同样的问题。

    更改为:

      String can="<tr>"+
                          "<td class=\"summaryinfo\">"+
    
                                "<div class=\"dinner\">1,000</div>" +
                                "<div style=\"margin-top:5px " +
                                 "font-weight:bold\">times</div>"+
                            "</td>"+
                        "</tr>";
    

    我不知道这是否修复了它,但它至少现在可以编译了。

        2
  •  1
  •   CaffGeek    16 年前

    但是,您的regex正在匹配(.*?)任何字符,任何重复次数,尽可能少”

    意思是,它什么都不匹配…什么都不匹配。

    …或者你的报价没有漏掉。

        3
  •  0
  •   Shlomi Fish    16 年前

    您应该使用HTML解析器来解析和处理HTML—而不是正则表达式。

        4
  •  0
  •   orithena    16 年前

    如前所述,您将需要转义内部的双引号 全部的 你的琴弦。

    如果你想得到“1000”,你需要使用 group(1) ,否则您将得到完全匹配的模式。

    结果代码:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class gfile
     {
      public static void main(String args[]) {
        // create a Pattern
        Pattern p = Pattern.compile("<div class=\"dinner\">(.*?)</div>");
    
        // create a Matcher and use the Matcher.group() method
        String can="<tr>"+
                              "<td class=\"summaryinfo\">"+
    
                                    "<div class=\"dinner\">1,000</div>" +
                                    "<div style=\"margin-top:5px " +
                                     "font-weight:bold\">times</div>"+
                                "</td>"+
                            "</tr>";
    
        Matcher matcher = p.matcher(can);
    
        if(matcher.find())
        {
           System.out.println(matcher.group(1));
        }
        else
           System.out.println("could not find");
      }
    }
    
        5
  •  -1
  •   Seaux    16 年前

    (.*?) 可能需要 (.*)?