If String contains String Pattern return Value

GoodJuJu

Member
Joined
Apr 26, 2010
Messages
24
Location
England
Programming Experience
5-10
Hi there, I wonder if somebody could help me here... I am struggling to extract a 'matched' string and wonder if I am missing something simple.

I have the following scenario....

Let's say my code finds a string.... 'abcdefgh -john.smith 1234567 a123-a-123-f the last search, abc, 123 ;'

I am searching this string for the pattern '[a-z]###-[a-z]-###-*'

I know this text appears in the string as 'a123-a-123-f'

I would like to be able to return the matched string value 'a123-a-123-f' to a variable.

If I place a wildcard * in front of the pattern, it will find the text, but obviously everything else in front of it, is there a way that somebody can think of to strip the variable without using the preceding wildcard?

I am not worried about the wildcard on the end of the string as I can strip back to the last space.



I am trying the code shown below, I don't really see why it won't work?!?!? The code itself is working with no errors, it just doesn't match the string.

*****************************

strReader = lcase("abcdefgh -john.smith 1234567 a123-a-123-f the last search, abc, 123 ;")
Dim re As New Regex(Lcase("[a-z]###-[a-z]-###-"))
Dim ma As Match = re.Match(strReader)

If ma.Success Then
' Print out the character position where a match was found.
Console.WriteLine("Found match at position " & ma.Index.ToString())
End If

*****************************

Many thanks for any help you can offer, I hope I have explained it clearly!!
 
Last edited:
Solved

I found the answer.....

I discovered that I cannot use '#' to search for numeric characters.

I have had to revise the search as shown below.....

From

[a-z]###-[a-z]-###-*

to

[a-z][0-9][0-9][0-9]-[a-z]-[0-9][0-9][0-9]-*

Thanks to anybody who looked into this.
 
Some comments
  • * has a special meaning (repeat zero or more times).
  • [0-9] can be substituted with \d (digits).
  • Repeating patterns has a stronghold in regular expressions, for example {n} to repeat n times.
  • You can also group with ().

Thus this pattern is searching for strings like "a123-z456-"
VB.NET:
([a-z]\d{3}-){2}
What your intension with the last * really was I cannot guess, in file system searches the * means 'anything', which in regex would be .+ (ie any char repeated one or more times, greedy).
I recommend you take some time with regex tutorials, for example at Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
 
John,

I was being blasé and asssumed that because I had used the same format in other programs it would work in vb.net / regex.

Thanks for your comments, some useful bits in there.
 
Back
Top