Click here to Skip to main content
15,740,620 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hello all,

I just found the handy operator Like to search for keywords in a string. Now, if there is a match, how would I extract just that match?

Say my string is

"A01B01 - My Stuff"

I can see that the string has A01B01 in it by using strMine Like "A##B##". It returns true. But how can I easily extract that info? I need some way to extract the A01B01 from that string. Also, the string can be "01x01" or "01-01" or any combination there of. I can find what type it is. I just test for every single combination and store the "A##B##" or what not in a string. But again, I need to extract that info.

I am betting there is a pretty easy way to do it but for the life of me I can't figure it out.

Prerak Patel 6-Mar-11 12:15pm    
Try to learn some regex, or I would show you how to do it tomorrow. Don't put your comments as answers.
Prerak Patel 6-Mar-11 22:37pm    
Check the updated answer.

You can use Regular expression

Public Function getDimention(ByVal input As String) As String
  Dim returnValue As String = ""
  Dim regex As New RegularExpressions.Regex("A(\d{2})B(\d{2})")
  Dim m As RegularExpressions.Match = regex.Match(input)
  If m IsNot Nothing Then
    returnValue = m.Groups(1).Value & "x" & m.Groups(2).Value
  End If
  Return returnValue
End Function
Share this answer
Ali Al Omairi(Abu AlHassan) 5-Mar-11 7:35am    
JBenhart said: "Can you elaborate? I don't know that much about that. "

This is how you can use Regular expression. Import the below namespace.


You can extract the string like

string output = string.Empty;

if(Regex.IsMatch("A01B01 - My Stuff", @"A01B01"))
     output = Regex.Match("A01B01 - My Stuff", @"A01B01").Value;
Share this answer
JBenhart 4-Mar-11 20:03pm    
But the numbers 01 (and 01) are unknown. I am using strMyString Like "A##B##" to see if it exists. Is there a way to use a wildcard with Regex.match?

output = Regex.Match(strMyString, @"A[0-9][0-9]B[0-9][0-9]").Value;

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