views:

169

answers:

5

I am getting the compile time error.

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");
  }
}
+1  A: 

But, your Regex is matching (.*?) "Any character, any number of repetitions, as few as possible"

Meaning, it matches nothing...and everything.

...or the fact that your quotes aren't escaped.

Chad
The (.*?) is valid. It matches zero or more characters of any value between the given two <div> elements. The "?" just makes that non-greedy so it locates the very next `</div>`.
Kevin Brock
A: 

(.*?) might need to be (.*)?

ocdcoder
+7  A: 

You have unescaped quotes inside your call to Pattern.compile.

Change:

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

To:

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

Note: I just saw the same problem in your String can.

Change it to:

  String can="<tr>"+
                      "<td class=\"summaryinfo\">"+

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

I don't know if this fixes it, but it will at least compile now.

Eric
A: 

You should use an HTML parser to parse and process HTML - not a regular expression.

Shlomi Fish
If I could vote it up 100 times I would
extraneon
This doesn’t answer the question.
Gumbo
@allthosewhocomplain about Regexes with HTML, XML, etc. It's bad if you have it unstructured, and the formatting can change. BUT, if you KNOW the format, and KNOW it won't have surprises, there is no reason not to use a Regex. Just because it's often not the correct course, doesn't mean it never is.
Chad
@Gumbo: The only question I see up there is "Can anybody tell me what's wrong with this code?". Assuming that we want to answer the implied questions ("What's wrong with this code?") other than a simple "Yes" answer, this is a perfectly good answer. The code is trying to parse and process HTML with regexes, and that is fundamentally what's wrong with it.
David Thornley
@David Thornley: girinie said, he/she is getting a compiler error. And using regular expressions to process HTML doesn’t cause compiler errors.
Gumbo
@Gumbo: I know. There is a minor thing wrong with the code that is causing a compiler error. The major thing wrong with it is that it uses regexes to parse HTML. I'm serious about this: telling people about minor flaws in trying to do the wrong thing isn't really helpful.
David Thornley
@David Thornley: girinie is asking what is causing the compiler error. Answering with “don’t use regular expressions to process HTML” is *not* an answer to *that* question. That’s what the comments are for.
Gumbo
A: 

As already pointed out, you'll need to escape the double quotes inside all of your strings.

And, if you want to have "1,000" as result, you'll need to use group(1), else you'll get the complete match of the pattern.

Resulting code:

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");
  }
}
maligree
No, you don't need to escape angle brackets.
Alan Moore
Thanks, fixed it...
maligree