2011-08-05 3 views
0

정규 표현식을 사용하여 한 줄에 내 패턴을 여러 개 찾으려고합니다. 참고 : 나는 ... 약 한 시간 동안 정규식을 사용하고 =/예를 들어Regex 표현식을 올바르게 가져 오는 데 도움이 필요합니다.

:

<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 

두 번 일치해야합니다 :

1) <a href="G2532" id="1">back</a> 
2) <a href="G2564" id="2">next</a> 

내가 생각하는 답이 적절한에있다 욕심쟁이 대 소유욕 대 숙달의 숙달 그러나 나는 그것을 작동시키지 않을 수 있습니다 ...

내가 가까이에 있다고 생각, 내가 만든 Regex 문자열은 다음과 같습니다 :

(<a href=").*(" id="1">).*(</a>) 

그러나 정규식 정규 1 경기, 전체 문자열을 반환...

나는 아래의 코드에서 (컴파일 가능한) 자바 정규식 테스트 장치가있다. 여기에이 프로그램을 사용하여 얻으려는 최근의 (쓸데없는) 시도가 있습니다. 출력은 꽤 직관적이어야합니다. 여기

Enter your regex: (<a href=").*(" id="1">).*(</a>) 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)+ 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (<a href=").*(" id="1">).*(</a>)? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: ((<a href=").*(" id="1">).*(</a>))? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 
I found the text "" starting at index 63 and ending at index 63. 

Enter your regex: ((<a href=").*(" id="1">).*(</a>))+? 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

Enter your regex: (((<a href=").*(" id="1">).*(</a>))+?) 
Enter input string to search: <a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a> 
I found the text "<a href="G2532" id="1">back</a> <a href="G2564" id="2">next</a>" starting at index 0 and ending at index 63. 

는 자바의 :

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class RegexTestHarness { 

    public static void main(String[] args){ 
     try{ 
      while (true) { 

       System.out.print("\nEnter your regex: "); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
       Pattern pattern = Pattern.compile(reader.readLine()); 

       System.out.print("Enter input string to search: "); 
       Matcher matcher = pattern.matcher(reader.readLine()); 

       boolean found = false; 
       while (matcher.find()) { 
        System.out.println("I found the text \"" + matcher.group() + "\" starting at " + 
         "index " + matcher.start() + " and ending at index " + matcher.end() + "."); 
        found = true; 
       } 
       if(!found){ 
        System.out.println("No match found."); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

    } 
} 
+1

[RegEx를 사용하여 HTML을 구문 분석하면 안됩니다.] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Bohemian

답변

1

보십시오이 :

<a href=".*?" id="1">.*?</a> 

나는 ?.*

그러나 경우에 추가하여 비 욕심에 캡처를 변환 한 의심스러운 점이 있다면이 트릭을 사용할 수 있습니다 :

<a href="[^"]*" id="1">[^<]*</a> 

은 큰 따옴표
[^<]*하지 않은 문자의 수를 의미 왼쪽 각도없는 문자의 수를 의미

그래서 당신은 욕심/비 욕심

에 대해 걱정하지 않도록
+0

보헤미안, 옳은 길로 나를 시작하게했습니다. 당신의 기술을 사용했지만, id = "1"을 "id ="[1-9] + "로 변경해야한다는 것을 깨달았습니다. 결국, 이제 작동합니다. 감사합니다. – Ryan

관련 문제