Home All Groups Group Topic Archive Search About

Q: Regular Expressions?

Author
23 Nov 2006 10:58 AM
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

Author
23 Nov 2006 11:17 AM
Robinson
Show quote Hide quote
"G .Net" <nodamnspam@email.com> wrote in message
news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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.



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
Author
23 Nov 2006 11:29 AM
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
---------------------------
Show quoteHide quote
"G .Net" <nodamnspam@email.com> wrote in message
news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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
>
Author
23 Nov 2006 12:00 PM
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

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com...
>        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 .Net" <nodamnspam@email.com> wrote in message
> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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
>>
>
Author
23 Nov 2006 3:05 PM
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:

Show quoteHide quote
> 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 [MVP]" <vb***@bellsouth.net> wrote in message
> news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com...
> >        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 .Net" <nodamnspam@email.com> wrote in message
> > news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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
> >>
> >
Author
23 Nov 2006 5:56 PM
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[i].Groups[1].Value);

Show quoteHide quote
On Thu, 23 Nov 2006 12:00:19 -0000, "G .Net" <nodamnspam@email.com>
wrote:

>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 [MVP]" <vb***@bellsouth.net> wrote in message
>news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com...
>>        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 .Net" <nodamnspam@email.com> wrote in message
>> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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
>>>
>>
>
--

Bits.Bytes.
http://bytes.thinkersroom.com
Author
23 Nov 2006 6:19 PM
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


On Thu, 23 Nov 2006 20:56:46 +0300, "Rad [Visual C# MVP]"
<nospam@nospam.com> wrote:

Show quoteHide quote
>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[i].Groups[1].Value);
>
>On Thu, 23 Nov 2006 12:00:19 -0000, "G .Net" <nodamnspam@email.com>
>wrote:
>
>>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 [MVP]" <vb***@bellsouth.net> wrote in message
>>news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com...
>>>        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 .Net" <nodamnspam@email.com> wrote in message
>>> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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
>>>>
>>>
>>
--

Bits.Bytes.
http://bytes.thinkersroom.com
Author
26 Nov 2006 12:15 PM
G .Net
Thanks guys for all your help. Most useful.

G

Show quoteHide quote
"Rad [Visual C# MVP]" <nospam@nospam.com> wrote in message
news:ehpbm2pd8m9vdqutj9baqq8ch547amdb9q@4ax.com...
> 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
>
>
> On Thu, 23 Nov 2006 20:56:46 +0300, "Rad [Visual C# MVP]"
> <nospam@nospam.com> wrote:
>
>>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[i].Groups[1].Value);
>>
>>On Thu, 23 Nov 2006 12:00:19 -0000, "G .Net" <nodamnspam@email.com>
>>wrote:
>>
>>>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 [MVP]" <vb***@bellsouth.net> wrote in message
>>>news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com...
>>>>        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 .Net" <nodamnspam@email.com> wrote in message
>>>> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.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
>>>>>
>>>>
>>>
> --
>
> Bits.Bytes.
> http://bytes.thinkersroom.com