Url 调整正则表达式以仅捕获 url 而不是 ip
Url regex tweak to just capture url not ip
我已经制作了这个正则表达式来捕获所有类型的 url
(它确实捕获了所有 url
),但它也捕获了单个 ip
.
这是我的场景:我有一个包含 IP、哈希和 url 的列表,我的 url 正则表达式和 ip 正则表达式都捕获相同的条目。不知道单个ip算不算“url”.
我的正则表达式:((http|https)://)?(www)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,9}\b([-a-zA-Z0-9()@:%_\|+.~#?&//={};,\[\]'"$\x60]*)?
捕获所有这些:
http://127.0.0.1/
http://127.0.0.1
https://127.0.0.1/m=weblogin/loginform238,363,771,89816356,2167
127.0.0.1:8080 ------> excluding this one is okay too (optional)
127.0.0.1 ------> i want to exclude this one
google.com
google.com:80
www.google.com
https://google.com
https://www.google.com
我希望我的正则表达式捕获除单个 ip 之外的所有 url,如下所示:
127.0.0.1
- 注意:我想在golang代码中使用这个(使用golang正则表达式引擎)
- 注意:我正在使用
regexp.Compile()
和FindAllString
函数。
您可以使用实现 "best trick ever" with FindAllStringSubmatch
的正则表达式:匹配您需要 skip/omit 的内容,并匹配和 捕获 您需要保留的内容。
\b(?:https?://)?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(?:[^:]|$)|((?:https?://)?(?:www)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,9}\b[-a-zA-Z0-9()@:%_\|+.~#?&//={};,\[\]'"$\x60]*)
第一个替代方案是 IP matching regex,其中我添加了 (?:https?://)?
部分以匹配可选的协议部分和 (?:[^:]|$)
部分以确保存在 [=16= 以外的字符] 或 IP 模式后的字符串结尾,但您可以进一步调整这部分。
然后,use it in Go喜欢
package main
import (
"fmt"
"regexp"
)
func main() {
r := regexp.MustCompile(`\b(?:https?://)?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(?:[^:]|$)|((?:https?://)?(?:www)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,9}\b[-a-zA-Z0-9()@:%_\|+.~#?&//={};,\[\]'"$\x60]*)`)
matches := r.FindAllStringSubmatch(`http://127.0.0.1/
http://127.0.0.1
http://www.127.0.0.1/m=weblogin/loginform238,363,771,89816356,2167
127.0.0.1:8080
127.0.0.1
google.com
google.com:80
www.google.com
https://google.com
https://www.google.com`, -1)
for _, v := range matches {
if (len(v[1]) > 0) { // if Group 1 matched
fmt.Println(v[1]) // Display it, else do nothing
}
}
}
输出:
http://www.127.0.0.1/m=weblogin/loginform238,363,771,89816356,2167
127.0.0.1:8080
google.com
google.com:80
www.google.com
https://google.com
https://www.google.com
我已经制作了这个正则表达式来捕获所有类型的 url
(它确实捕获了所有 url
),但它也捕获了单个 ip
.
这是我的场景:我有一个包含 IP、哈希和 url 的列表,我的 url 正则表达式和 ip 正则表达式都捕获相同的条目。不知道单个ip算不算“url”.
我的正则表达式:((http|https)://)?(www)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,9}\b([-a-zA-Z0-9()@:%_\|+.~#?&//={};,\[\]'"$\x60]*)?
捕获所有这些:
http://127.0.0.1/
http://127.0.0.1
https://127.0.0.1/m=weblogin/loginform238,363,771,89816356,2167
127.0.0.1:8080 ------> excluding this one is okay too (optional)
127.0.0.1 ------> i want to exclude this one
google.com
google.com:80
www.google.com
https://google.com
https://www.google.com
我希望我的正则表达式捕获除单个 ip 之外的所有 url,如下所示:
127.0.0.1
- 注意:我想在golang代码中使用这个(使用golang正则表达式引擎)
- 注意:我正在使用
regexp.Compile()
和FindAllString
函数。
您可以使用实现 "best trick ever" with FindAllStringSubmatch
的正则表达式:匹配您需要 skip/omit 的内容,并匹配和 捕获 您需要保留的内容。
\b(?:https?://)?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(?:[^:]|$)|((?:https?://)?(?:www)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,9}\b[-a-zA-Z0-9()@:%_\|+.~#?&//={};,\[\]'"$\x60]*)
第一个替代方案是 IP matching regex,其中我添加了 (?:https?://)?
部分以匹配可选的协议部分和 (?:[^:]|$)
部分以确保存在 [=16= 以外的字符] 或 IP 模式后的字符串结尾,但您可以进一步调整这部分。
然后,use it in Go喜欢
package main
import (
"fmt"
"regexp"
)
func main() {
r := regexp.MustCompile(`\b(?:https?://)?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(?:[^:]|$)|((?:https?://)?(?:www)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,9}\b[-a-zA-Z0-9()@:%_\|+.~#?&//={};,\[\]'"$\x60]*)`)
matches := r.FindAllStringSubmatch(`http://127.0.0.1/
http://127.0.0.1
http://www.127.0.0.1/m=weblogin/loginform238,363,771,89816356,2167
127.0.0.1:8080
127.0.0.1
google.com
google.com:80
www.google.com
https://google.com
https://www.google.com`, -1)
for _, v := range matches {
if (len(v[1]) > 0) { // if Group 1 matched
fmt.Println(v[1]) // Display it, else do nothing
}
}
}
输出:
http://www.127.0.0.1/m=weblogin/loginform238,363,771,89816356,2167
127.0.0.1:8080
google.com
google.com:80
www.google.com
https://google.com
https://www.google.com