我尝试创建正则表达式以匹配部分网址 可能的URL可能是
www.mysite.com?userid=123xy
www.mysite.com?userid=123x&username=joe
www.mysite.com?tag=xyz&userid=1ww45
www.mysite.com?tag=xyz&userid=1g3x5&username=joe
我试图匹配userid=123456
到目前为止我已经
了Dim r As New Regex("[&?]userID.*[?&]")
Debug.WriteLine(r.Match(strUrl))
但这仅匹配第2和第4行。 有人可以帮忙吗?
答案 0 :(得分:1)
(?<=[?&]userid=)[^&#\s]*
输出:
123xy
123x
1ww45
1g3x5
几点:
www.mysite.com?tag=xyz&userid=1ww45#top
)userid
的情况无关紧要,请使用RegexOptions.IgnoreCase
。答案 1 :(得分:0)
我明白了: [&安培;?] =用户ID [^ \ S&安培;#] +
答案 2 :(得分:0)
PHP解决方案:
"/[\\?&]userid=([^&]*)/"
试验:
$tests = [
[
"regex" => "/[\\?&]userid=([^&]*)/",
"expected" => "123xy",
"inputs" => [
"www.mysite.com?userid=123xy",
"www.mysite.com?userid=123xy&username=joe",
"www.mysite.com?tag=xyz&userid=123xy",
"www.mysite.com?tag=xyz&userid=123xy&username=joe"
]
]
];
foreach ($tests as $test) {
$regex = $test['regex'];
$expected = $test['expected'];
foreach ($test['inputs'] as $input) {
if (!preg_match($regex, $input, $match)) {
throw new Exception("Regex '{$regex}' doesn't match for input '{$input}' or error has occured.");
}
$matched = $match[1];
if ($matched !== $expected) {
throw new Exception("Found '{$matched}' instead of '{$expected}'.");
}
echo "Matched '{$matched}' in '{$input}'." . PHP_EOL;
}
}
结果:
Matched '123xy' in 'www.mysite.com?userid=123xy'.
Matched '123xy' in 'www.mysite.com?userid=123xy&username=joe'.
Matched '123xy' in 'www.mysite.com?tag=xyz&userid=123xy'.
Matched '123xy' in 'www.mysite.com?tag=xyz&userid=123xy&username=joe'.
答案 3 :(得分:-1)
您可以使用正则表达式:.*?(userid=\d+).*
.*?
- 是一种非贪婪的表达方式:(userid=\d+)
之前的所有内容
Python示例:
import re
a = 'www.mysite.com?userid=12345'
b = 'www.mysite.com?userid=12345&username=joe'
mat = re.match('.*?(userid=\d+).*', a)
print mat.group(1) # prints userid=12345
mat = re.match('.*?(userid=\d+).*', b)
print mat.group(1) # prints userid=12345
链接到 Fiddler