kservice Posted August 12, 2004 Posted August 12, 2004 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. Quote
Arch4ngel Posted August 12, 2004 Posted August 12, 2004 [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. Quote "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
neodammer Posted August 13, 2004 Posted August 13, 2004 Dim oldstr As String = "Session73Number" oldstr = oldstr.Remove(0, 7) oldstr = oldstr.Remove(2, 6) MsgBox(oldstr) That would end you with just 73 ..if that helps any. Quote Enzin Research and Development
John_0025 Posted August 16, 2004 Posted August 16, 2004 [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 Quote
kservice Posted August 16, 2004 Author Posted August 16, 2004 Thanks a bunch I kind of figured out the same thing but your example gives me a much cleaner solution. Thanks again! :-\ Quote
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.