Get 2 Strings

S

shapper

Hello,

I have a string which has the following format (I give 2 examples):

"FirstWord468x60SecondWord"
"FirstWord5x172SecondWord"

I need to get the numbers 468 and 60, or 5 and 172 into strings
FirstNumber and SecondNumber.

How can I do this?

Can I use a Regex?

Thanks,
Miguel
 
G

Guest

Hello,

I have a string which has the following format (I give 2 examples):

"FirstWord468x60SecondWord"
"FirstWord5x172SecondWord"

I need to get the numbers 468 and 60, or 5 and 172 into strings
FirstNumber and SecondNumber.

How can I do this?

Can I use a Regex?

Thanks,
Miguel

yes, you can use a regex.

For example:

using System.Text.RegularExpressions;

Regex regex = new Regex(
@"(\d*)",
RegexOptions.Compiled
);
 
S

shapper

yes, you can use a regex.

For example:

using System.Text.RegularExpressions;

Regex regex = new Regex(
@"(\d*)",
RegexOptions.Compiled
);

Hello,

I tried the following:

Dim a As String = "something128x98word"

Dim b() As String = Regex.Split(a, "(\d*)")

For Each s As String In b
Response.Write(s)
Next

This is not working.
Could you tell me what am I doing wrong?

Thanks,
Miguel
 
G

Guest

Hello,

I tried the following:

Dim a As String = "something128x98word"

Dim b() As String = Regex.Split(a, "(\d*)")

For Each s As String In b
Response.Write(s)
Next

This is not working.
Could you tell me what am I doing wrong?

Thanks,
Miguel- Hide quoted text -

- Show quoted text -

The Regex.Split method splits the string at a delimiter, for example,
if you change a regular expression to match into "x"

Dim b() As String = Regex.Split(a, "x")

you will get "something128" and "98word"

If you need to get a numbers, use following

Dim a As String = "something128x98word"

Dim r As Regex = New Regex("(\d+)") ' I changed it to {+}

For Each m As Match In r.Matches(a)
Response.Write(m.Groups.Item(1).ToString())
Next
 
G

Guest

Hello,

I tried the following:

Dim a As String = "something128x98word"

Dim b() As String = Regex.Split(a, "(\d*)")

For Each s As String In b
Response.Write(s)
Next

This is not working.
Could you tell me what am I doing wrong?

Thanks,
Miguel- Hide quoted text -

- Show quoted text -

I think, you can use the Split method too:

Change the regular expression to

Regex.Split(a, "[A-Za-z]+")

and check if string is not empty

For Each s As String In b
if s.Length()>0 then
Response.Write(s)
end if
Next
 
R

Rad [Visual C# MVP]

Hello,

I tried the following:

Dim a As String = "something128x98word"

Dim b() As String = Regex.Split(a, "(\d*)")

For Each s As String In b
Response.Write(s)
Next

This is not working.
Could you tell me what am I doing wrong?

Thanks,
Miguel

Try this:

Regex r = new Regex(@"(?<number>\d+)");
MatchCollection m =
r.Matches("FirstWord468x60SecondWord");


The match collection will give you the matches found in the string.

So for the string, m.Count should return 2.

To access the individual values use their indexes

m[0].Value should give you 468
m[1].Value should give you 60
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top