简体   繁体   中英

Invalid escape sequence for regex to match URL in Java

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.

Note that in Java strings you have to escape backslashes (ie \\ would result in the string literal "\\\\" ). Thus the expression should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM