简体   繁体   中英

how to write regex for this expression in java

I'am working on a regex str to match a expression: #acontent#url. the expression start with "#a", and the "content" can be almost things, text,num or others, then end with "#" + url.

such as there is a text content: hello, man, this is something #ayou want# http://www.stackoverflow.com .

It will be match the string "#ayou want# http://www.stackoverflow.com ", I really don't konw how to write the regex.

Need some help please!

The short answer:

#a.+#((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[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/

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