我有一个html页面。我只想提取那些在句子/文本末尾带有问号的标签中的文本。我使用:
<.+?>(.+?)<.+?>来获取标记内的文本。但这样做有两个问题: 1-所有嵌套的标签也会被提取出来,这是我不想要的。(我只想要纯文本)2-我只想得到那些在标签末尾有一个问号的文本。
我不知道该怎么做。有没有人能帮我(用爪哇)。PS:我的html页面格式不正确,因此,使用诸如JSoup之类的工具不是一个好的选择。这就是为什么我只使用正则表达式。
发布于 2012-08-17 18:13:47
发布于 2012-08-17 22:59:49
你有充分的理由使用正则表达式吗?
你可以自己分析你的html代码。也许它会更快。如果在<mytag?>和</mytag?>中没有任何标记,这里有一个小的解决方案
final LinkedList<String> chunks = new LinkedList<String>();
final String text = "<i>italic</i><mytag?>text</mytag?><href>anchor</href> <mySecondTag?>word</mySecondTag?>";
String rest = text;
int pos;
while ( (pos = rest.indexOf("?>") )!=-1)
{
final int endTag = rest.indexOf("<", pos);
chunks.add(rest.substring(pos+2, endTag));
rest = rest.substring(rest.indexOf(">", endTag+1)+1);
}
System.out.println(chunks);https://stackoverflow.com/questions/12003743
复制相似问题