简体   繁体   中英

Regex to match URL within long string

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.

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 .

Try something like this:

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

More Information :

您可以尝试此正则表达式:

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

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

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