Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need to extract a number out of a string.

 

Ex: Session73Number

 

Should I and could I use a regular expression and if so what would it look like.

 

In case you can't tell, I've never written a regular expression. :rolleyes:

 

Thanks in advance.

Posted

[0-9]? will determine that any number will be catched.

 

However... it's been a week or two since I've done Regex... and I'm not a master in that section...

 

Try this :

 

.*([0-9]?).*

 

And see .Capture(...) in your regex. It might have captured "73" somewhere. If it didn't... well... keep working... you are on the right track.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

[0-9]{1,} will match the numbers in your string.

 

The code below will return an array of all the numbers in your string.

 

i.e. "Session73Number" will return "73"

"Session73Number532" will return "73" and "532"

 

   Public Shared Function ReturnNumericValues(ByVal Text As String) As ArrayList
       Dim myRegExp As New System.Text.RegularExpressions.Regex("[0-9]{1,}", RegexOptions.IgnoreCase)
       Dim Matches As System.Text.RegularExpressions.MatchCollection
       Matches = myRegExp.Matches(Text)
       Dim myMatch As System.Text.RegularExpressions.Match
       Dim matchedValues As New ArrayList

       For Each myMatch In Matches
           matchedValues.Add(myMatch.Value)
       Next

       return matchedValues
   End Function

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...