1

I try to write regex to match URL in Java. I found the regex on the internet and it works fine in the online regex editor. When i try to run it on the eclipse, it gives following error : Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?

How can i solve this problem?

Thanks in advance.

4

1 回答 1

4

请注意,在Java 字符串中,您必须转义反斜杠(即\会产生字符串文字"\\")。因此,表达式应如下所示:

String expression = "(http|https):\\/\\/(\\w+:{0,1}\\w*@)?(\\S+)(:[0-9]+)?(\\/|\\/([\\w#!:.?+=&%@!\\-\\/]))?";
于 2012-04-25T13:08:18.240 回答