我想匹配以下字符串:
<a href="http://xyz">xyz</a>
<a href="https://xyz">xyz</a>
<a href="xyz">xyz</a>
Run Code Online (Sandbox Code Playgroud)
我尝试使用正则表达式但失败了:
<a href="((http://|https://|).+)">\1</a>
Run Code Online (Sandbox Code Playgroud)
实现这一目标的正确解决方案是什么?谢谢!
以下正则表达式:
<a\s+href="(?:https?://)?([^"]+)">\1</a>
Run Code Online (Sandbox Code Playgroud)
一个解释:
<a\s+href=" # match `<a href="`
(?:https?://)? # optionally match `http://` or `https://`
([^"]+) # match one or more chars other than `"`, and store it in group 1
"> # match `">`
\1 # match the same as group 1
</a> # match `</a>`
Run Code Online (Sandbox Code Playgroud)
Java演示:
public class Main {
public static void main(String[] args) {
String[] tests = {
"<a href=\"http://xyz\">xyz</a>",
"<a href=\"https://xyz\">xyz</a>",
"<a href=\"xyz\">xyz</a>",
"<a href=\"xyz\">xyzzz</a>"
};
String regex = "<a\\s+href=\"(?:https?://)?([^\"]+)\">\\1</a>";
for(String test : tests) {
System.out.println(test.matches(regex));
}
}
}
Run Code Online (Sandbox Code Playgroud)
打印:
true
true
true
false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1281 次 |
| 最近记录: |