-6

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!

2

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.