Regular expressions and matching URLs with metacharacters
I'm having trouble finding a regular expression that matches the following String.
Korben;http://feeds.feedburner.com/KorbensBlog-UpgradeYourMind?format=xml;1
One problem is escaping the question mark. Java's pattern matcher doesn'开发者_开发技巧t seem to accept \?
as a valid escape sequence but it also fails to work with the tester at myregexp.com.
Here's what I have so far:
([a-zA-Z0-9])+;http://([a-zA-Z0-9./-]+);[0-9]+
Any suggestions?
Edit: The original intent was to match all URLs that could be found after the first semi colon.
If you are putting the expression in a string, you need to escape the "\" as well. That is:
String expr = "([a-zA-Z0-9])+;http://([a-zA-Z0-9./\\-\\?]+);[0-9]+";
You also need to escape the "-" if it's not the last character in a character class ([...]) construct.
[?]
matches "?"
Maybe you need to escape your backslash, if your expression is in a string. Something like "\\?"
([a-zA-Z0-9]+);http://([a-zA-Z0-9./-]+)(\?[^;]+);([0-9]+)
Works for me on that RexExp Editor website.
精彩评论