如何检查给定字符串是否为有效的URL地址?
我对正则表达式的了解是基本的,不允许我从我在网上看到的数百个正则表达式中进行选择.
哪些字符使网址无效?
这些有效的网址是?
example.com/file[/].htmlhttp://example.com/file[/].html在尝试学习更多有关正则表达式的内容时,教程建议您可以使用它\b来匹配单词边界.但是,Python解释器中的以下代码段无法按预期工作:
>>> x = 'one two three'
>>> y = re.search("\btwo\b", x)
Run Code Online (Sandbox Code Playgroud)
它应该是一个匹配对象,如果匹配任何东西,但它是None.
是\b不是在Python支持或正在使用它错了,我的表达?
我正在寻找一个接受这样的网址的正则表达式:
http://www.example.com
www.example.com
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止,但正则表达式不匹配URL没有http://或https://,或ftp://:
regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
Run Code Online (Sandbox Code Playgroud)
如何使协议可选?
我试图在Javascript中使用John Gruber的URL正则表达式,但NetBeans一直告诉我存在语法错误和非法错误:
var patt = "/(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])
|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]
{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|
(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|
(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:
'".,<>?«»“”‘’]))/";
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
您好,我想使用单个reguar表达式验证www.google.com或http://www.google.com或google.com类型的网址,是否可以实现,如果是这样,请在javascript中共享解决方案.请注意我只希望底层协议是HTTP或HTTPS morover现在的主要问题是我们如何使用Javascript中的单个正则表达式映射所有这三个模式,它不必检查页面是否处于活动状态,如果用户输入的值与上面列出的三个案例中的任何一个匹配,那么另一方面它应该返回true,如果它没有它应该返回fasle.
我需要用户能够创建部分URL,而无需每次都输入完整的URL.
例如,假设我计划在网址上www.example.com/areas/{userinput}/home.我希望用户能够在文本框中输入一些文本,我想验证它是否是有效的URL.
URL验证器的功能如下:
url: function(value, element) {
return this.optional(element) || /giantregex/.test(value);
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下方法向验证器添加验证方法:
$.validator.addMethod("exturl", function(value) {
return $.validator.methods.url.call($.validator, "http://www.example.com/areas/" + value + "/home");
});
Run Code Online (Sandbox Code Playgroud)
但是,当表单被验证并且我的扩展名被调用时,我不断收到此错误:
Uncaught TypeError: Object function (d,a){this.settings=b.extend(true,{},b.validator.defaults,d);this.currentForm=a;this.init()} has no method 'optional'
Run Code Online (Sandbox Code Playgroud)
optional看起来是$.validator对象的方法,但我无法弄清楚如何调用验证器url方法.
我尝试在文本中获取URL.所以,在此之前,我使用了这样一个表达式:
let re = NSRegularExpression(pattern: "https?:\\/.*", options: nil, error: nil)!
Run Code Online (Sandbox Code Playgroud)
但是当用户输入带有大写符号的URL时(例如Http://Google.com,它与它不匹配),我遇到了问题.
我试过了:
let re = NSRegularExpression(pattern: "(h|H)(t|T)(t|T)(p|P)s?:\\/.*", options: nil, error: nil)!
Run Code Online (Sandbox Code Playgroud)
但什么都没发生.
好吧,我想要一种方法来使用 puppeteer 和 for 循环来获取站点上的所有链接并将它们添加到数组中,在这种情况下,我想要的链接不是 html 标签中的链接,它们是链接直接在源代码、javascript 文件链接等中......我想要这样的东西:
array = [ ]
for(L in links){
array.push(L)
//The code should take all the links and add these links to the array
}
Run Code Online (Sandbox Code Playgroud)
但是如何获取对网站源代码中的 javascript 样式文件和所有 URL 的所有引用?我只是找到一个帖子和一个问题,教或展示它如何从标签中获取链接,而不是从源代码中获取所有链接。
假设您想获取此页面上的所有标签,例如:
查看源:https : //www.nike.com/
如何获取所有脚本标签并返回控制台?我view-source:https://nike.com之所以这样说是因为您可以获得脚本标签,我不知道您是否可以在不显示源代码的情况下做到这一点,但是我考虑过显示和获取脚本标签,因为这是我的想法,但是我不知道如何去做吧
我正在和socketio聊天.每次发送消息时,我都会使用这个非常简单的jquery显示它:
$('#try').prepend(my_message);
Run Code Online (Sandbox Code Playgroud)
有:
<div id='try'></div>
Run Code Online (Sandbox Code Playgroud)
我想要做的是查找发布的消息是否包含链接,如果是,则使其可点击.我需要找到http://和www.
我发现了几个相关的问题,但没有一个问题给了我正在寻找的解决方案.
关于如何实现这一点的任何想法?
我需要从使用以下值创建的 DataFrame 列中提取 URL
\n\ncreation_date,tweet_id,tweet_text\n2020-06-06 03:01:37,1269102116364324865,#Webinar: Sign up for @SumoLogic\'s June 16 webinar to learn how to navigate your #Kubernetes environment and unders\xe2\x80\xa6 /sf/ask/2956636651/\n2020-06-06 01:29:38,1269078966985461767,"In this #webinar replay, @DisneyStreaming\'s @rothgar chats with @SumoLogic\'s @BenoitNewton about how #Kubernetes is\xe2\x80\xa6 /sf/ask/3285004551/\nRun Code Online (Sandbox Code Playgroud)\n\n列名称tweet_text包含 URL。我正在尝试以下代码。
df["tweet_text"]=df["tweet_text"].astype(str)\npattern = r\'https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)\'\n\ndf[\'links\'] = \'\'\ndf[\'links\']= df["tweet_text"].str.extract(pattern, expand=True)\n\nprint(df)\nRun Code Online (Sandbox Code Playgroud)\n\n我正在使用这个问题的答案中的正则表达式,它与两行中的 URL 匹配。
\n但我得到的NaN是新列的值df[\'links]\'。我也尝试过这个问题的第一个答案中提供的解决方案,即
df[\'links\']= df["tweet_text"].str.extract(pattern, expand=False).str.strip()\nRun Code Online (Sandbox Code Playgroud)\n\n但我收到以下错误
\n\nAttributeError: \'DataFrame\' object has no attribute \'str\'\n …Run Code Online (Sandbox Code Playgroud) 我有一个包含HTML内容的数据库,它有一些带链接的文本.有些文本的URL中有哈希符号,有些则没有.
我需要删除带有哈希符号的链接,保留那些没有哈希符号的链接.
例:
输入:
<a href="http://example.com/books/1">The Lord of the Rings</a>
<ul>
<li><a href="http://example.com/books/1#c1" >Chapter 1</a></li>
<li><a name="name before href" href="http://example.com/books/1#c2">Chapter 2</a></li>
<li><a href="http://example.com/books/1#c3" name="name after href">Chapter 3</a></li>
<li><a href="http://example.com/books/1#cN" target="_blank">Chapter N</a></li>
</ul>
<br><br>
<a href="http://example.com/books/1">Harry Potter</a>
<ul>
<li><a href="http://example.com/books/2#c1" target="_self">Chapter 1</a></li>
<li><a href="http://example.com/books/2#c2" name="some have name" title="some others have title" >Chapter 2</a></li>
<li><a href="http://example.com/books/2#c3">Chapter 3</a></li>
<li><a href="http://example.com/books/2#cN" >Chapter N</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
期望的输出:
<a href="http://example.com/books/1">The Lord of the Rings</a>
<ul>
<li>Chapter 1</li>
<li>Chapter 2</li>
<li>Chapter 3</li>
<li>Chapter N</li>
</ul>
<br><br> …Run Code Online (Sandbox Code Playgroud)