I've this three text, and one regexp. (OK, it's HTML, but ...please, don't focus on it !!!!)
<h3 class="pubAdTitleBlock "><a href="/it/pubblicazioni/libri/Che-speranza-cè-per-i-morti/1101987030/" title="Che speranza c’è per i morti?">Che speranza c’è per i morti? (volantino N. 16)</a></h3>
<h3 class="pubAdTitleBlock "><a href="/it/pubblicazioni/libri/cosa-insegna-la-bibbia/È-questo-che-Dio-voleva/" title="È questo che Dio voleva?">Cosa insegna realmente la Bibbia?</a></h3>
<h3 class="pubAdTitleBlock">Cantiamo a Geova</h3>
This is the regexp
regexp = "<h3[^>]*>(<a[^>]*>)?([^<]+)(</a>)?</h3>";
I've three 3 groups:
- the opening
<a>
tag (optional) - the text (it's a book title, it's the goal of regexp)
- the closing
</a>
tag (optional)
Problem: The second row is matched, the third is matched. The first no. Why ?
Matching code:
pattern = Pattern.compile(regexp);
matcher = pattern.matcher(fullString);
idx = 0;
while (matcher.find()) {
...
}
matcher.find()
simply skips the first row. This is not the first row of the file, it's the 10th. It's the first of the example.
Can be the literal parenthesis the problem? how to fix the regexp ?
EDIT: I've tried
String regexp = "<h3[^>]*>(.+)</h3>";
But also this regexp skip the first row ... I really cannot understand !!!!
EDIT 2:
I'm having a dubt: can be a problem if there is the accented charcter ?
EDIT 3:
I'm trying to do data scraping from here: http://www.jw.org/it/pubblicazioni/libri/?contentLanguageFilter=it&sortBy=3
I've an input stream, then I convert to a single string using this code:
// copied from http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
public static String convertStreamToString(InputStream is) {
try {
return new java.util.Scanner(is, "UTF-8").useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
Then I'm apllying the regexp ...