Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式帮助需要匹配字符串末尾的符号and或_Regex_Vb.net - Fatal编程技术网

Regex 正则表达式帮助需要匹配字符串末尾的符号and或

Regex 正则表达式帮助需要匹配字符串末尾的符号and或,regex,vb.net,Regex,Vb.net,我正在尝试创建一个正则表达式来匹配URL的一部分 可能的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("[&?]

我正在尝试创建一个正则表达式来匹配URL的一部分 可能的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行。
有人能帮忙吗?

您可以使用regex:
*?(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
链接到

我知道了: [&?]userID=[^\s&#]+

(?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'.

[?&]
=>
(&|$)
,但大多数语言都有更强大的url和参数解析,您或许可以使用。@SteveGray在您写的问题中:“尝试匹配userid=123456”,现在在(删除答案的)注释中,您写的是相反的…那么是哪一种?预期的输出是什么?
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'.