我试图得到一个正则表达式来查找我的模式的多个条目在一行上。注意:我已经使用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>)
但是Regex匹配器返回1匹配,整个字符串…
我有一个(可编译的)Java正则表达式测试工具在下面的代码。这是我最近(徒劳的)尝试使用该程序,输出应该是相当直观的。
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.
Java:
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);
}
}
}
试试这个:
<a href=".*?" id="1">.*?</a>
我通过在.*
?
将捕获转换为非贪婪但是当你有疑问的时候,你可以使用这个技巧:
<a href="[^"]*" id="1">[^<]*</a>
[^"]*
表示非双引号的任意数量的字符[^<]*
表示非左角的任意字符
所以你不用担心贪心/非贪心