Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Leaders
Posted
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.
--tim
Posted

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??!!??

Posted

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.

.nerd
Posted
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???
  • Leaders
Posted

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}"

--tim
Posted

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!

  • Leaders
Posted

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

--tim

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...