-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!

naiyu
  • 745
  • 3
  • 9
  • 25
  • See here https://stackoverflow.com/questions/4430076/regex-in-java-group-matching?rq=1 – MASL Nov 21 '15 at 07:29
  • 2
    "I really don't konw how to write the regex." Then you should learn http://www.tutorialspoint.com/java/java_regular_expressions.htm – Madushan Perera Nov 21 '15 at 07:30

1 Answers1

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/

Community
  • 1
  • 1
Timothy
  • 26
  • 6