Re: [問題] java 正規取代表示法
看板RegExp (正規表示式 Regular Expression)作者appleboy46 (我是道明柏毅)時間17年前 (2008/03/10 11:44)推噓0(0推 0噓 0→)留言0則, 0人參與討論串2/4 (看更多)
※ 引述《appleboy46 (我是道明柏毅)》之銘言:
: 我有一個字串如下:
: <li>( ?url = "LAB221"^^xsd:string )</li>
: 我只要中間的 LAB221 的部份,所以我就用底下的正規,可是比對不出來
: 可以請大家幫忙看看嗎?
: 感謝大家
: // compile pattern
: Pattern p = Pattern.compile("<li>( \?[\\w+] = \"([A-Za-z0-9]+)\".* )</li>");
: // get matcher
: Matcher m = p.matcher("<li>( ?url = \"LAB221\"^^xsd:string )</li>");
: // test if match
: if (m.matches()) {
: System.out.println(m.group(1));
: }
: else
: {
: System.out.println("error");
: }
我解出來了~:
// compile pattern
Pattern p = Pattern.compile("[^\"]+\"([^\"]+)\"[^\"]+");
// get matcher
Matcher m = p.matcher(result.replaceAll("[<li></li>]",""));
// test if match
if (m.matches()) {
System.out.println(m.group(1));
}
else
{
System.out.println("error");
}
請問大家,還有更好的解法嗎?
感謝 qrtt1
--
********************************************************
* Appleboy Blog *
* http://blog.Wu-Boy.com/ *
* Appleboy Album *
* http://pic.wu-boy.com *
********************************************************
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.215.42
※ 編輯: appleboy46 來自: 140.123.215.42 (03/10 11:45)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 4 篇):
RegExp 近期熱門文章
PTT數位生活區 即時熱門文章