Note that there are some explanatory texts on larger screens.

plurals
  1. POJava regex lazy operator not so lazy?
    primarykey
    data
    text
    <p>I have Java class that have to fetch the content of an URL online (returning an XML), and apply a regexp over it (the behaviour is defined by third-party files, I so cannot use DOM or SAX to parse the response). Here is the code :</p> <pre><code> import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[] args) { try { StringBuffer buffer = new StringBuffer(); URL url = new URL("http://api.themoviedb.org/2.1/Movie.search/en/xml/57983e31fb435df4df77afb854740ea9/Inglourious+Bastards"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.connect(); InputStream input = conn.getInputStream(); for(int c = input.read(); c != -1; c = input.read()) buffer.append((char)c); Pattern pattern = Pattern.compile("&lt;movie&gt;.*?&lt;name&gt;([^&lt;]*)&lt;/name&gt;.*?&lt;id&gt;([^&lt;]*)&lt;/id&gt;.*?&lt;/movie&gt;", Pattern.DOTALL); Matcher matcher = pattern.matcher(buffer); for(int i = 1; i &lt; (matcher.groupCount() + 1); i++) { matcher.find(); String toReplace = matcher.group(i); System.out.println(toReplace); } } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>Its output is for this sample "Inglourious Basterds" and then "22311", which is the content of the name tag in the first movie tag, and the content of the id tag in the <strong>second</strong> movie tag. However, the lazy operator should guarantee that it is only the items in the first movie tag that are retrieved.</p> <p>Moreover, the following code in python, which basically does exactly the same, works in the expected way.</p> <pre><code>import re import urllib url = urllib.urlopen("http://api.themoviedb.org/2.1/Movie.search/en/xml/" "57983e31fb435df4df77afb854740ea9/Inglourious+Bastards") m = re.search("&lt;movie&gt;.*?&lt;name&gt;([^&lt;]*)&lt;/name&gt;.*?&lt;id&gt;([^&lt;]*)&lt;/id&gt;.*?&lt;/movie&gt;", url.read(), re.DOTALL) print m.group(1), m.group(2) </code></pre> <p>Any idea on what is wrong with my code ?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload