繁体   English   中英

java和regexp:如何将字符串与lithreal括号匹配?

[英]java and regexp: how to match a string with lithreal parenthesis?

我有这三个文本和一个正则表达式。 (好的,它是HTML,但是……请不要专注于它!!!)

<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>

这是正则表达式

regexp = "<h3[^>]*>(<a[^>]*>)?([^<]+)(</a>)?</h3>";

我分为三个3组:

  • 开头的<a>标记(可选)
  • 文本(这是书名, 这是regexp的目标
  • 结束</a>标记(可选)

问题 :第二行匹配,第三行匹配。 第一个 为什么呢

匹配代码:

pattern = Pattern.compile(regexp);
matcher = pattern.matcher(fullString);
idx = 0;
while (matcher.find()) {
  ...
}

matcher.find()只是跳过第一行。 这不是文件的第一行,而是第十行。 这是示例的第一个。

可能是括号内的问题吗? 如何修复正则表达式?

编辑 :我已经尝试过

String regexp = "<h3[^>]*>(.+)</h3>";

而且这个正则表达式也跳过了第一行...我真的听不懂!

编辑2:

我有一个疑问:如果有重音符号可能会出现问题吗?

编辑3:

我正在尝试从此处进行数据抓取: http ://www.jw.org/it/pubblicazioni/libri/?contentLanguageFilter=it&sortBy =3

我有一个输入流,然后使用以下代码将其转换为单个字符串:

 // 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 "";
    }

然后我正在使用正则表达式...

不确定,但是也许这就是您要寻找的

String data = "<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>";

Pattern pattern = Pattern
        .compile("<h3[^>]*>(?:<a[^>]*>)?([^<]+)(?:</a>)?</h3>");
Matcher matcher = pattern.matcher(data);
while (matcher.find()) 
    System.out.println(matcher.group(1));

输出:

Che speranza c’è per i morti? (volantino N. 16)
Cosa insegna realmente la Bibbia?
Cantiamo a Geova

小解释:

诸如(?:someregex)类的组不会被正则表达式机制计算在内。 多亏了(?:a)(b)(?:c)(d)组, (b)索引为1, (d)索引为2。

EDIT1

(我知道使用正则表达式来解析HTML是一种亵渎行为,但是由于OP希望这么做...)
您忘了提到,已解析的HTML在<h3 >内包含制表符和换行符之类的空白。 尝试这种方式:

String data = convertStreamToString(new URL(
        "http://www.jw.org/it/pubblicazioni/libri/?contentLanguageFilter=it&sortBy=3")
        .openStream());

Pattern pattern = Pattern
        .compile("<h3[^>]*>\\s*(?:<a[^>]*>)?([^<]+)(?:</a>)\\s*?</h3>");
Matcher matcher = pattern.matcher(data);
int counter=0;
while (matcher.find())
    System.out.println(++counter +")"+matcher.group(1));

输出:

1)Accostiamoci a Geova
2)Accostiamoci a Geova — caratteri grandi
....
11)Cosa insegna realmente la Bibbia?
12)Cosa insegna realmente la Bibbia? — caratteri grandi

不要使用解析器或RegExp。 尝试杰瑞 喜欢(未经测试):

Jerry doc = jerry(html);
doc.$("a").each(new JerryFunction() {
    public boolean onNode(Jerry $this, int index) {
        String href = $this.attr("href");
        System.out.println(href);
    }
}

或任何html友好的查询语言。 由于非外部要求,请尝试尝试使用Java解析HTML目录列表中的链接

(从以下位置复制我的答案: 如何使用Java从html解析链接?

编辑:尝试

<h3.*?>(<a.*)?+(.*?)(</a>)?</h3>

得到组(2)

编辑2:只为书名尝试:

(.*>)?([^<]+?)<.*

编辑3:您的正则表达式

<h3[^>]*>(<a[^>]*>)?([^<]+)(</a>)?</h3>

看起来为我工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM