Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Can anyone tell me how can I get the matched string in my specified pattern.

For example I used this
VB
Dim myString as String = "Hi! My name is XYZ"
Regex.IsMatch(".* My name is XYZ", myString)


Here the string will be matched. Is there any way which can say what string was matched with wildcard...

For example, In the above case Hi! will be matched with .* wildcard.. Is there any way to detect it.

Thanks in advance
Posted

1 solution

Your parameters are the wrong way round - the first parameter is the string to test, and the second parameter is the regular expression to use:
VB.NET
Regex.IsMatch(myString, ".* My name is XYZ")

To retrieve the details of the match, use the Match method[^], which returns a Match object[^]. You'll also need to wrap the section you want to retrieve in a group: (.*)
VB.NET
Dim myString As String = "Hi! My name is XYZ"
Dim match As Match = Regex.Match(myString, "(.*) My name is XYZ")
If match.Success Then
    Dim matchedPart As String = match.Groups(1).Value
    ...
End If

Regular Expression Language - Quick Reference[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 17-Dec-15 16:36pm    
Sure, a 5.
—SA
Irfan Project 18-Dec-15 0:16am    
Thanks Richard Deeming... Your solution helped me a lot! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900