简体   繁体   English

正则表达式以匹配长字符串中的URL

[英]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. 美好的一天,我想以$ 1000(例如,删除这些括号)$ test $ asdf [并删除这些括号]在此处查询3D产品名称的价格。 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 目前,我们在这里还有另一个产品名称,我相信它的准确性约为0.0005 199200。http ://www.example.com这是一个测试

I have a regex linked here: https://regex101.com/r/yE4kW6/7 我在这里链接了一个正则表达式: 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. 我现在要尝试的是使其与整个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? 看来我的|https?:\\/\\/.\\-*\\s正在找到该url,但它却忽略了它,可能是因为该集开头的^是? I could use some help having it match URLs within a string. 我可以使用一些帮助来匹配字符串中的URL。

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): 对于JavaScript,这可以归结为(注意转义的正斜杠):

(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). 这不是很具体,并且很大程度上取决于您的输入字符串(也可以匹配URL的无效字符)。 See a demo on regex101 here . 在此处查看regex101上的演示

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 https://regex101.com/r/yE4kW6/8进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM