Asp.net regex,其中输入在字符串末尾有&char

Asp.net regex,其中输入在字符串末尾有&char,asp.net,.net,regex,regular-language,Asp.net,.net,Regex,Regular Language,我正在使用这个正则表达式: .*-p(.\d+)-fun$ .*-p.-d+-fun\b意思是: 我的测试: http://example.com/abcd-p48343-fun Matched http://example.com/abcd-p48343-funab not matched http://example.com/abcd-p48343-fun&ab=1 Matched 为什么最后一次测试匹配 字符串末尾的

我正在使用这个正则表达式:

.*-p(.\d+)-fun$
.*-p.-d+-fun\b意思是:

我的测试:

http://example.com/abcd-p48343-fun             Matched 
http://example.com/abcd-p48343-funab           not matched
http://example.com/abcd-p48343-fun&ab=1        Matched 
为什么最后一次测试匹配

字符串末尾的char似乎将它们分隔为两个字符串。什么是正则表达式不匹配的解决方案http://example.com/abcd-p48343-fun&ab=1 ?

.*-p.-d+-fun$也已测试但不工作。

此正则表达式:

.*-p(.\d+)-fun$
仅与第一个示例匹配:

VB.Net代码:

Dim Tests As New List(Of String)
Dim Pattern As String
Dim Parser As Regex

Tests.Add("http://example.com/abcd-p48343-fun")
Tests.Add("http://example.com/abcd-p48343-funab")
Tests.Add("http://example.com/abcd-p48343-fun&ab=1")

Pattern = ".*-p(.\d+)-fun\b"
Parser = New Regex(Pattern)
Console.WriteLine("Using pattern: " & Pattern)
For Each Test As String In Tests
    Console.WriteLine(Test & " : " & Parser.IsMatch(Test).ToString)
Next
Console.WriteLine()

Pattern = ".*-p(.\d+)-fun$"
Parser = New Regex(Pattern)
Console.WriteLine("Using pattern: " & Pattern)
For Each Test As String In Tests
    Console.WriteLine(Test & " : " & Parser.IsMatch(Test).ToString)
Next
Console.WriteLine()

Console.ReadKey()
控制台输出:

Using pattern: .*-p(.\d+)-fun\b
http://example.com/abcd-p48343-fun : True
http://example.com/abcd-p48343-funab : False
http://example.com/abcd-p48343-fun&ab=1 : True

Using pattern: .*-p(.\d+)-fun$
http://example.com/abcd-p48343-fun : True
http://example.com/abcd-p48343-funab : False
http://example.com/abcd-p48343-fun&ab=1 : False

它不匹配:@PawełŁukasik-OP使用的是.net风格的正则表达式,而不是PCRE@PawełŁukasik看看这个@Moslem7026,您的测试示例是错误的,或者使用多行选项表示您当时对一行感兴趣: