0

could you please someone tell me how to match these types of url in string?

For example:

example.com
www.example.com
http://example.com
https://www.example.com
example.com/exam1/exam.php
example.com/exam
...

Actually, I need to detect all forms of URL you can see for example on Twitter in plain text.

I have tried to find some regex by google but I couldn't find something which fits all the types in Java.

UPDATE: Maybe I wasn't too precise - I need find all these types of url in plain text and replace them by some token. For example: status.replaceAll(yourRegex, "URL");

Thank you!

Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
Petr Jirásek
  • 453
  • 1
  • 5
  • 17

2 Answers2

3
^(?:https?:\/\/)?(?:www\.)?[a-zA-Z0-9./]+$

Try this.See demo.

https://www.regex101.com/r/fG5pZ8/24

Use

^(?:https?:\/\/)?(?:www\.)?example\.[a-zA-Z0-9./]+$

If you want to match example. something.See demo.

https://www.regex101.com/r/fG5pZ8/26

vks
  • 67,027
  • 10
  • 91
  • 124
  • It works well but I need detect these urls in plain text (maybe I wasn't too precise in my explanation)) and replace by url token... something like this: status.replaceAll(yourRegex, "_URL_"); – Petr Jirásek Jan 03 '15 at 17:34
1

This should work reasonably well to match any URL:

(https?://)?\w+(\.\w+)+(/\w+)*(/\w+\.\w+)?(\?[\w%&=.]*)*(?=[^\w.?&%=])

See demo

It matches any number of path parts too, eg `example.com/a/b/c?x=y&a=b

Bohemian
  • 412,405
  • 93
  • 575
  • 722