繁体   English   中英

使用Matcher和Regex进行Java解析

[英]Java parsing with Matcher and Regex

我有一个非常简单的问题,但我是Java Matcher的新手,我很难弄清楚如何将它用于我的具体问题。

我有一个类似this <not needed content>src="url"<not needed content>src="url2"<not needed content>的字符串this <not needed content>src="url"<not needed content>src="url2"<not needed content>

其中<'不需要的内容'>是我想在字符串中忽略的内容。 我基本上想从字符串中提取URL。

我的代码目前看起来像这样

Pattern MY_PATTERN = Pattern.compile("\\src=\"(.*?)\\\"");
Matcher m = MY_PATTERN.matcher(content);
String s = "something";
while (m.find()) {
   s = m.group(1);
}

我为这样一个基本的,可能是重复的问题道歉。

谢谢。

你为什么不尝试简单的模式? 像这个 :

Pattern.compile("src=\"(.*?)\"");

(未经测试,但应该更好)

您可以使用以下任一正则表达式:

src="([^"]+)
src="(.+?"

暂无
暂无

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

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