how to write regex for this expression in java

The short answer:

#a.+#((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w][email protected])?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w][email protected])[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)

This will match any character except line breaks in the content section, and should match any url.

NOTE: You will have to escape all backslashes in the given regex to represent it as a java String literal.

Sources for the url regex:

What is the best regular expression to check if a string is a valid URL?

http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without

Fantastic resource for testing and building regexes:

http://www.regexr.com/

Browse More Popular Posts

Leave a Comment