Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract numeric part of string? 3

Status
Not open for further replies.

lunargirrl

Programmer
Jan 26, 2004
77
BR
Hi people :)
Is there a way to extract only the numeric part of a string?
e.g. extract only the number '15' from carlajones15 and the number '208' from lisafaye208 ???
Thanks,
Gis.
 
Use regular expressions. The reg exp you are after will be \d+

Mark [openup]
 
try this
Code:
MatchCollection mc;
Regex r = new Regex("\\d+"); //one or more digits 
mc = r.Matches(_input);
// Loop through the match collection to retrieve all 
matches
for (int i = 0; i < mc.Count; i++) 
{
    Console.WriteLine("value " + mc[i].Value);
}
hth,
Marty
 
Hey marty, didnt understand your example very clear :(
I am working with vb.net, do you mind to write this example in vb.net, please?

Thanks a lot
Gis.
 
VB

Code:
Dim r As New RegularExpressions.Regex("\d+")
Dim mc As RegularExpressions.MatchCollection
mc = r.Matches("d09543434")
Dim i As Integer = 0
Do While i < mc.Count
  Response.Write("value " & mc(i).Value)
  i = i + 1
Loop
Response.End()
 
It worked Checkai. :)))

These are the regular expressions, quite difficult for a beginner in this like me :) I am reading articles from 4guysfromrolla and they are quite interesting.
I mentioned in my first post that I needed to separate the numerics from the string, but I need also to retrieve the character part as well (in my example keep in a variable carlajones and lisafaye), if I use \\d+ for the numerics, what could I use for the characters?? I dint find that at 4guysfromrolla. :/ Someone could point me a tut where the things are more clear and explain better where to get the character part of a string?

Thanks people, you are really nice to me !!
Gis
 
Dim Str as String
dim letters as string
dim nbrs as string
STR = "dlc0974"

Dim r As New RegularExpressions.Regex("\d+")
Dim mc As RegularExpressions.MatchCollection
mc = r.Matches("d09543434")
Dim i As Integer = 0
Do While i < mc.Count
Response.Write("value " & mc(i).Value)
nbrs = mc(i).Value
letters = replace(str,nbrs,'')
Response.Write(letters)
Response.Write(nbrs)
i = i + 1
Loop
Response.End()

would this work??

dlc
 
try this,
Code:
string[] s_array = Regex.Split(_input,"\\d+"); 
for (int i = 0; i < s_array.Length; i++) 
{
Console.WriteLine("value NOT in regex " + s_array[i]);
}
this will give you an array so for your case if the input is lisafaye208 s_array with i = 0 would be lisafaye

Please excuse the C# isadore posted a very usefull conversion link.
hth,
Marty
 
Thanks :))
Checkai, I used your example, it was easier to understand for me:

Dim Str As String
Dim letters As String
Dim nbrs As String

Dim r As New RegularExpressions.Regex("\d+")
Dim mc As RegularExpressions.MatchCollection
mc = r.Matches(Me.txtUser.Text)
Dim i As Integer = 0
Do While i < mc.Count
nbrs = mc(i).Value
letters = Replace(Str, nbrs, "")
i = i + 1
Loop
Me.lblText.Text = letters

When i print nbrs it returns the numeric part, perfect, but when i print the var letters, it returns nothing.

Thanks a lot and sorry for answering late.
Gis.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top