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