2010-03-24 4 views
0

다음 코드에 대한 정규식 작성 방법을 알고 싶습니다.이 경우 정규식을 작성하는 방법은 무엇입니까?

<a href="https://stackoverflow.com/search?q=user:111111+[apple]" class="post-tag" title="show all posts by this user in 'apple'">Apple</a><span class="item-multiplier">&times;&nbsp;171</span><br> 

위의 소스 코드에서 Apple을 가져와야합니다.

+6

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – kennytm

+0

KennyTM , 당신이 정규식을 사용하여 HTML을 파싱 할 수 없다는 것은 사실이지만,이 질문은 HTML을 파싱하는 것이 아니라 오히려 태그를 파싱하는 것입니다. 확실히 가능합니다. –

답변

1

txt2re에는 다양한 언어로 쉽게 정규 표현식을 생성하는 데 사용할 수있는 훌륭한 도구가 있습니다. 내가 generate에 다음을 사용 :

import java.util.regex.*; 

class Main 
{ 
    public static void main(String[] args) 
    { 
    String txt="<a href=\"https://stackoverflow.com/search?q=user:111111+[apple]\" class=\"post-tag\" title=\"show all posts by this user in 'apple'\">Apple</a><span class=\"item-multiplier\">&times;&nbsp;171</span><br>"; 

    String re1=".*?"; // Non-greedy match on filler 
    String re2="(?:[a-z][a-z]+)"; // Uninteresting: word 
    String re3=".*?"; // Non-greedy match on filler 
    String re4="(?:[a-z][a-z]+)"; // Uninteresting: word 
    String re5=".*?"; // Non-greedy match on filler 
    String re6="(?:[a-z][a-z]+)"; // Uninteresting: word 
    String re7=".*?"; // Non-greedy match on filler 
    String re8="((?:[a-z][a-z]+))"; // Word 1 

    Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8,Pattern.CASE_INSENSITIVE | Pattern.DOTALL); 
    Matcher m = p.matcher(txt); 
    if (m.find()) 
    { 
     String word1=m.group(1); 
     System.out.print("("+word1.toString()+")"+"\n"); 
    } 
    } 
} 
관련 문제