Leeus Posted April 22, 2003 Posted April 22, 2003 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? Quote
Leaders quwiltw Posted April 22, 2003 Leaders Posted April 22, 2003 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. Quote --tim
Leeus Posted April 22, 2003 Author Posted April 22, 2003 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??!!?? Quote
Heiko Posted April 22, 2003 Posted April 22, 2003 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. Quote .nerd
Leeus Posted April 22, 2003 Author Posted April 22, 2003 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??? Quote
Leaders quwiltw Posted April 22, 2003 Leaders Posted April 22, 2003 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}" Quote --tim
Leeus Posted April 22, 2003 Author Posted April 22, 2003 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! Quote
Leaders quwiltw Posted April 22, 2003 Leaders Posted April 22, 2003 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. 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 Quote --tim
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.