Q: Regular Expressions?

G

G .Net

Hi

I was wondering if you could help me with the following:

I have a string e.g. "#54x454#,#22b6#,#885333#".

I want to be able to store in an array list, the following from the string:

54x454
22b6
885333

That is, the items between the hash signs.

I believe this could be done with regular expressions?

Can anybody show me some code to achieve this. By the way, there may be more
than three items.

Thanks in advance

G
 
R

Robinson

G .Net said:
Hi

I was wondering if you could help me with the following:

I have a string e.g. "#54x454#,#22b6#,#885333#".

I want to be able to store in an array list, the following from the
string:

54x454
22b6
885333

That is, the items between the hash signs.

I believe this could be done with regular expressions?

Can anybody show me some code to achieve this. By the way, there may be
more than three items.



Are all of the items comma delimited and bracketed with # signs? Are there
leading or trailing spaces (i.e. between the commas?). If the format is
strictly adhered to, something simple like this will work:


Dim theString As String = "#234B#,#23324#,#23423ddd#,#23423#"

Dim theItems() As String = theString.Split(","c)

For i As Integer = 0 To theItems.Length - 1
theItems(i) = theItems(i).Substring(1, theItems(i).Length - 2)
Next
 
K

Ken Tucker [MVP]

Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s)
Next

Ken
 
G

G .Net

Thanks guys

Looks promising!

Could I bother you again. What if the characters I'm interested are enclosed
in [ and ]. For example,

"[54x454],[22b6],[885333]"

G
 
N

Nenefta

You can use the trim function then I guess:

Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s.Trim("[", "]")
Next


Greets,
Marcel


G .Net schreef:
Thanks guys

Looks promising!

Could I bother you again. What if the characters I'm interested are enclosed
in [ and ]. For example,

"[54x454],[22b6],[885333]"

G

Ken Tucker said:
Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s)
Next

Ken
 
R

Rad [Visual C# MVP]

Hey G,

Try this code:

//
// Create our regex
//
Regex r = new Regex(@"\[(?<Number>.*?)\]");
//
// Setup our capture string
//
string test = @"[54x454],[22b6],[885333]";
//
// Get our matches
//
MatchCollection m = r.Matches(test);
//
// Do with them as we will!
//
for(int i = 0; i<m.Count;i++)
Console.WriteLine(m.Groups[1].Value);

Thanks guys

Looks promising!

Could I bother you again. What if the characters I'm interested are enclosed
in [ and ]. For example,

"[54x454],[22b6],[885333]"

G

Ken Tucker said:
Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s)
Next

Ken
 
R

Rad [Visual C# MVP]

Sorry, forgot to context switch!

Here is the code in VB

'
' Create our regex
'
dim r as new Regex("\[(?<Number>.*?)\]")
'
' Setup our capture string
'
dim test as string = "[54x454],[22b6],[885333]"
'
' Get our matches
'
dim m as MatchCollection = r.Matches(test)
'
' Do with them as we will!
'
for i as integer = 0 to m.Count-1
Console.WriteLine(m(i).Groups(1).Value)
next


Hey G,

Try this code:

//
// Create our regex
//
Regex r = new Regex(@"\[(?<Number>.*?)\]");
//
// Setup our capture string
//
string test = @"[54x454],[22b6],[885333]";
//
// Get our matches
//
MatchCollection m = r.Matches(test);
//
// Do with them as we will!
//
for(int i = 0; i<m.Count;i++)
Console.WriteLine(m.Groups[1].Value);

Thanks guys

Looks promising!

Could I bother you again. What if the characters I'm interested are enclosed
in [ and ]. For example,

"[54x454],[22b6],[885333]"

G

Ken Tucker said:
Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s)
Next

Ken
---------------------------
Hi

I was wondering if you could help me with the following:

I have a string e.g. "#54x454#,#22b6#,#885333#".

I want to be able to store in an array list, the following from the
string:

54x454
22b6
885333

That is, the items between the hash signs.

I believe this could be done with regular expressions?

Can anybody show me some code to achieve this. By the way, there may be
more than three items.

Thanks in advance

G
 
G

G .Net

Thanks guys for all your help. Most useful.

G

Rad said:
Sorry, forgot to context switch!

Here is the code in VB

'
' Create our regex
'
dim r as new Regex("\[(?<Number>.*?)\]")
'
' Setup our capture string
'
dim test as string = "[54x454],[22b6],[885333]"
'
' Get our matches
'
dim m as MatchCollection = r.Matches(test)
'
' Do with them as we will!
'
for i as integer = 0 to m.Count-1
Console.WriteLine(m(i).Groups(1).Value)
next


Hey G,

Try this code:

//
// Create our regex
//
Regex r = new Regex(@"\[(?<Number>.*?)\]");
//
// Setup our capture string
//
string test = @"[54x454],[22b6],[885333]";
//
// Get our matches
//
MatchCollection m = r.Matches(test);
//
// Do with them as we will!
//
for(int i = 0; i<m.Count;i++)
Console.WriteLine(m.Groups[1].Value);

Thanks guys

Looks promising!

Could I bother you again. What if the characters I'm interested are
enclosed
in [ and ]. For example,

"[54x454],[22b6],[885333]"

G

Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s)
Next

Ken
---------------------------
Hi

I was wondering if you could help me with the following:

I have a string e.g. "#54x454#,#22b6#,#885333#".

I want to be able to store in an array list, the following from the
string:

54x454
22b6
885333

That is, the items between the hash signs.

I believe this could be done with regular expressions?

Can anybody show me some code to achieve this. By the way, there may
be
more than three items.

Thanks in advance

G
 

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

Top