2017-02-27 3 views
0

J'utilise cette expression rationnelle:regex où l'entrée a et le caractère à la fin de la chaîne

.*-p(.\d+)-fun\b sens:

.* => any char at the beginning, 
-p => static string , 
(.\d+) => number in first group, 
-fun => static string , 
\b => end of string , 

Mes tests:

http://example.com/abcd-p48343-fun    Matched 
http://example.com/abcd-p48343-funab   not matched 
http://example.com/abcd-p48343-fun&ab=1  Matched 

Pourquoi le dernier match de test?

Il semble & char à la fin de la chaîne séparez-les en deux cordes. Quelle est la solution que regex ne correspond pas http://example.com/abcd-p48343-fun&ab=1?

.*-p(.\d+)-fun$ également testé et ne fonctionne pas.

+0

Il ne correspond pas à : https://regex101.com/r/H9mzsp/1 –

+0

@ PawełŁukasik - OP utilise .net saveur regex pas PCRE –

+0

@ PawełŁukasik Regardez ceci http://regexr.com/3fd4o – Moslem7026

Répondre

0

Ce regex:

.*-p(.\d+)-fun$ 

Matchs le premier exemple que:

Code 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() 

Sortie de la console:

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