String Finding

Leeus

Regular
Joined
Dec 23, 2002
Messages
50
Hi all I have a number of variable length enclosed in <<nnnn>>

I can't seem to get this number out of the string, all attempts to do a search replace don't work, all I want is a value nnnn.

Any ideas?
 
I'm kinda rusty at regular expressions, but something like this should match it. "[0-9]+" It may need to get more complex depending on how the string is.
 
Sorry I should have said but it is in the middle of other stuff as well, so the string will look like,
"This is some text <<789798>> and I need that number!"

Hope that helps someone??!!??
 
Apart from the regular expressions why not do it in two steps ?

a) Get everything right of "<<"
b) In the result, get everything left of ">>"

Then convert to number.
 
What functions shall i use to do that, because the number is not a fixed length I cannot see a way to use the left or right functions???
 
That actually still works for this. If there are other numbers in the string that don't have the "<< >>" then that could present some issues. If that's the case, "<+[0-9]+>+" this will find the entire string like this "<<898989>>" and then maybe a subsequent "[0-9]+" on that would get the number in the middle. Someone that's good at regex's could probably craft a way to get just the number in one step -- obviously, that someone isn't me:)

I think regex's are probably your best bet.

Small improvement = "<{2}[0-9]+>{2}"
 
Anyone got a good idea on how to use regex's???

i haven't a clue, is there a good guide around, mode I look at seem a little ambiguous!
 
Apparently all you have to do is group them using parenthesis, then pull out the matches using the Groups collection of the match. The Groups collection appears to go like this:
0 - Returns the Whole expression
1 - Returns the first sub-expression
2 - Returns the second sub-expression
and so on...

This does what you want.
Visual Basic:
        Dim source As String = "(<{2})([0-9]+)(>{2})"
        Dim s As String = "Hello world <<898989>> hello world "
        Dim reg As New Regex(source)
        Dim r As String
        r = reg.Match(s, source).Groups(2).Value
 
Back
Top