Could java regex match a string which exists or not?
I want to match the following strings:
<a href="http://xyz">xyz</a>
<a href="https://xyz">xyz</a>
<a href="xyz">xyz</a>
and I tried to use the regex but failed:
<a href="((http://|https://|).+)">\1</a>
What is th开发者_运维知识库e correct solution to achieve this? Thanks!
The following regex does that:
<a\s+href="(?:https?://)?([^"]+)">\1</a>
An explanation:
<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>`
A Java demo:
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));
}
}
}
prints:
true
true
true
false
I think that is what you need:
<a href="(https?://)?(.+)">\2</a>
精彩评论