정규 표현식을 사용하여 한 줄에 내 패턴을 여러 개 찾으려고합니다. 참고 : 나는 ... 약 한 시간 동안 정규식을 사용하고 =/예를 들어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);
}
}
}
[RegEx를 사용하여 HTML을 구문 분석하면 안됩니다.] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Bohemian
있음 'href'와''태그 사이에 문자/숫자 만 기대합니까? – Josh