1

I have a string here:

Good Day,I would like to $1000 (for example remove these parenthesis) $test $ asdf [and remove these brackets] inquire the price of the 3D product name here. Currently we have the other product name here which I believe has an accuracy of about 0.0005 199 200. http://www.example.com this is a test

I have a regex linked here: https://regex101.com/r/yE4kW6/7

Which has this regex in it:

[^\w\s|https?:\/\/.\-*\s]|\W*\b\d+(?:\.\d+)?\b

What I'm trying to do now is have it match the entire URL. It seems that my |https?:\/\/.\-*\s is finding the url, but it's ignoring it, possible because the ^ at the start of that set? I could use some help having it match URLs within a string.

mariocatch
  • 8,305
  • 8
  • 50
  • 71
  • It's a test string :) And `https?` should capture both? – mariocatch Jan 20 '16 at 15:56
  • It depends on your input strings really. You could as well come up with sth. like `~(https?://\S+)~`, see [a demo here](https://regex101.com/r/aI4uW3/2), though not for JavaScript, admittedly (your question is tagged with different languages anyway). – Jan Jan 20 '16 at 16:05

4 Answers4

1

In addition to my comment, you could come up with a regex like:

~(https?://\S+)~
# start / end with the tilde ~ as delimiters
# look for http/https, followed by ://
# match anything that is not a whitespace
# capture everything into a group

For JavaScript this would come down to (mind the escaped forward slashes):

(https?:\/\/\S+)

This is not very specific and heavily depends on your input strings, though (aka invalid characters for a URL can be matched as well). See a demo on regex101 here.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • Thank you, but how would I combine it with the regex I had initially linked? Simply adding `(https?:\/\/\S+)` to it doesn't add that support. This is the initial regex I had: `[^\w\s]|\W*\b\d+(?:\.\d+)?\b` https://regex101.com/r/yE4kW6/9 – mariocatch Jan 20 '16 at 16:34
  • @mariocatch: What exactly are you trying to do? The above regex simply matches a string starting with `http://` or `https://`. Your initial request was to match an URL which it does. Putting everybit of logic into one regex makes it unreadable in most of the cases. – Jan Jan 20 '16 at 16:39
  • I'm trying to take everything that https://regex101.com/r/yE4kW6/9#javascript has, and also have it match the URL within that string. Remove special characters, allow the term `3D` to pass, remove money amounts, and remove URLs. I'm not concerned with a readable regex, as I'll just have to apply multiple rules to the same string in the end anyways. May as well do it all at once. – mariocatch Jan 20 '16 at 16:42
  • Looks like this regex works, adding yours to what I had: `(https?:\/\/\S+)|[^\w\s]|\b\d+(?:\.\d+)?\b` Seen here: https://regex101.com/r/lS4gQ4/1 – mariocatch Jan 20 '16 at 16:49
  • @mariocatch: Glad to help :-) – Jan Jan 20 '16 at 16:54
0

Try something like this:

[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

More Information:

Community
  • 1
  • 1
Farukh
  • 2,173
  • 2
  • 23
  • 38
0

You could try this Regex :

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
Pogrindis
  • 7,755
  • 5
  • 31
  • 44
0

You can use the following regex

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

I have tested it with you string. Test it at https://regex101.com/r/yE4kW6/8

STORM
  • 4,005
  • 11
  • 49
  • 98
  • This removes the initial regex logic I had. My issue was combining what I had with the URL matcher. https://regex101.com/r/yE4kW6/9 – mariocatch Jan 20 '16 at 16:35
  • What are you trying to do in complete? – STORM Jan 20 '16 at 16:35
  • I'm trying to take everything that https://regex101.com/r/yE4kW6/9 has, and also have it match the URL within that string. Remove special characters, allow the term `3D` to pass, remove money amounts, and remove URLs. – mariocatch Jan 20 '16 at 16:36