Home All Groups Group Topic Archive Search About

Regex.Split... Can I do this??

Author
16 Oct 2006 1:28 PM
Jordi Rico
Hi,

I know I can split a string into an array doing this:

Dim s As String()=Regex.Split("One-Two-Three","-")

So I would have:

s(0)="One"
s(1)="Two"
s(2)="Three"

The problem is, I am receiving some kind of data this way:

"*One#*Two#*Three#", it is, every word starts with "*" and ends with
"#"...

Is there any way of using Regex to split it like the previous example??

Thanks in advance

Author
16 Oct 2006 2:32 PM
Ron Weiner
How about
    strIn = replace(strIn,"*","")
    Dim s As String()=Regex.Split(strIn,"#")
Show quoteHide quote
"Jordi Rico" <jordir***@gmail.com> wrote in message
news:1161005330.270951.227920@i42g2000cwa.googlegroups.com...
> Hi,
>
> I know I can split a string into an array doing this:
>
> Dim s As String()=Regex.Split("One-Two-Three","-")
>
> So I would have:
>
> s(0)="One"
> s(1)="Two"
> s(2)="Three"
>
> The problem is, I am receiving some kind of data this way:
>
> "*One#*Two#*Three#", it is, every word starts with "*" and ends with
> "#"...
>
> Is there any way of using Regex to split it like the previous example??
>
> Thanks in advance
>
Author
16 Oct 2006 5:42 PM
Jordi Rico
No, 'cos it would give a result like this:

s(0)="One"
s(1)=""
s(2)="Two"
s(3)=""
s(4)="Three"

But thanks anyway


Ron Weiner ha escrito:

Show quoteHide quote
> How about
>     strIn = replace(strIn,"*","")
>     Dim s As String()=Regex.Split(strIn,"#")
> --
> Ron W
> www.WorksRite.com
> "Jordi Rico" <jordir***@gmail.com> wrote in message
> news:1161005330.270951.227920@i42g2000cwa.googlegroups.com...
> > Hi,
> >
> > I know I can split a string into an array doing this:
> >
> > Dim s As String()=Regex.Split("One-Two-Three","-")
> >
> > So I would have:
> >
> > s(0)="One"
> > s(1)="Two"
> > s(2)="Three"
> >
> > The problem is, I am receiving some kind of data this way:
> >
> > "*One#*Two#*Three#", it is, every word starts with "*" and ends with
> > "#"...
> >
> > Is there any way of using Regex to split it like the previous example??
> >
> > Thanks in advance
> >
Author
16 Oct 2006 9:09 PM
Jeff Dillon
Actually his solution works. You have ONE blank array entry at the end, but
you could safely ignore that one.

Jeff
Show quoteHide quote
"Jordi Rico" <jordir***@gmail.com> wrote in message
news:1161020552.983779.144580@m73g2000cwd.googlegroups.com...
> No, 'cos it would give a result like this:
>
> s(0)="One"
> s(1)=""
> s(2)="Two"
> s(3)=""
> s(4)="Three"
>
> But thanks anyway
>
>
> Ron Weiner ha escrito:
>
>> How about
>>     strIn = replace(strIn,"*","")
>>     Dim s As String()=Regex.Split(strIn,"#")
>> --
>> Ron W
>> www.WorksRite.com
>> "Jordi Rico" <jordir***@gmail.com> wrote in message
>> news:1161005330.270951.227920@i42g2000cwa.googlegroups.com...
>> > Hi,
>> >
>> > I know I can split a string into an array doing this:
>> >
>> > Dim s As String()=Regex.Split("One-Two-Three","-")
>> >
>> > So I would have:
>> >
>> > s(0)="One"
>> > s(1)="Two"
>> > s(2)="Three"
>> >
>> > The problem is, I am receiving some kind of data this way:
>> >
>> > "*One#*Two#*Three#", it is, every word starts with "*" and ends with
>> > "#"...
>> >
>> > Is there any way of using Regex to split it like the previous example??
>> >
>> > Thanks in advance
>> >
>
Author
16 Oct 2006 9:20 PM
Chris
Hi Jordi,

I know this isn't as straightforward as Regex.Split(), but it does exactly
what you want.

Begin code:
===================================
imports Microsoft.VisualBasic
imports System
imports system.Text.RegularExpressions
imports System.Collections

public module MyModule
Sub Main()
Dim myItems as New ArrayList
myItems = getValues("*One#*Two#*Three#")

  For each item as string in myItems
   Console.WriteLine( item & VbCrLf)
  next

  Console.ReadLine()
end sub

    'Here is where the real work gets done
    Function getValues(ByVal Input As String) As ArrayList
        Dim RegexObj As String = "\*(?<ValueIwant>.+?)\#"
        Dim options As RegexOptions = RegexOptions.None
        Dim matches As MatchCollection = Regex.Matches(Input, RegexObj,
options)
        Dim myMatchArray As New ArrayList

        For Each foundItem As Match In matches
            myMatchArray.Add(foundItem.Groups("ValueIwant").Value)
        Next

        'You could skip the above loop and access
        'found items directly. For example:
        'matches.Item(i).Groups("ValueIwant").Value
        'Where i is an integer >= 0

        Return myMatchArray
    End Function

end module
===================================
End Code.

I don't think that using the Split function is the way to go here because
you have two possible characters to match. You could use an expression like:
(\*\#|\*) but I don't know if that would confuse the Split function. You can
try.

I hope this helped.

Chris


Show quoteHide quote
"Jordi Rico" <jordir***@gmail.com> wrote in message
news:1161005330.270951.227920@i42g2000cwa.googlegroups.com...
> Hi,
>
> I know I can split a string into an array doing this:
>
> Dim s As String()=Regex.Split("One-Two-Three","-")
>
> So I would have:
>
> s(0)="One"
> s(1)="Two"
> s(2)="Three"
>
> The problem is, I am receiving some kind of data this way:
>
> "*One#*Two#*Three#", it is, every word starts with "*" and ends with
> "#"...
>
> Is there any way of using Regex to split it like the previous example??
>
> Thanks in advance
>
Author
17 Oct 2006 9:47 AM
Jordi Rico
Thanks a lot Chris, in fact that's the solution I could find searching
everywhere!
I'm absolutely new in the world of Regex, and, although it's really
hard, it is going to be very useful for our new project, as I have to
parse a lot of diferent incoming messages... so now I'm going to study
the basis of these methods...


Chris ha escrito:

Show quoteHide quote
> Hi Jordi,
>
> I know this isn't as straightforward as Regex.Split(), but it does exactly
> what you want.
>
> Begin code:
> ===================================
> imports Microsoft.VisualBasic
> imports System
> imports system.Text.RegularExpressions
> imports System.Collections
>
> public module MyModule
>  Sub Main()
>  Dim myItems as New ArrayList
>  myItems = getValues("*One#*Two#*Three#")
>
>   For each item as string in myItems
>    Console.WriteLine( item & VbCrLf)
>   next
>
>   Console.ReadLine()
>  end sub
>
>     'Here is where the real work gets done
>     Function getValues(ByVal Input As String) As ArrayList
>         Dim RegexObj As String = "\*(?<ValueIwant>.+?)\#"
>         Dim options As RegexOptions = RegexOptions.None
>         Dim matches As MatchCollection = Regex.Matches(Input, RegexObj,
> options)
>         Dim myMatchArray As New ArrayList
>
>         For Each foundItem As Match In matches
>             myMatchArray.Add(foundItem.Groups("ValueIwant").Value)
>         Next
>
>         'You could skip the above loop and access
>         'found items directly. For example:
>         'matches.Item(i).Groups("ValueIwant").Value
>         'Where i is an integer >= 0
>
>         Return myMatchArray
>     End Function
>
> end module
> ===================================
> End Code.
>
> I don't think that using the Split function is the way to go here because
> you have two possible characters to match. You could use an expression like:
> (\*\#|\*) but I don't know if that would confuse the Split function. You can
> try.
>
> I hope this helped.
>
> Chris
>
>
> "Jordi Rico" <jordir***@gmail.com> wrote in message
> news:1161005330.270951.227920@i42g2000cwa.googlegroups.com...
> > Hi,
> >
> > I know I can split a string into an array doing this:
> >
> > Dim s As String()=Regex.Split("One-Two-Three","-")
> >
> > So I would have:
> >
> > s(0)="One"
> > s(1)="Two"
> > s(2)="Three"
> >
> > The problem is, I am receiving some kind of data this way:
> >
> > "*One#*Two#*Three#", it is, every word starts with "*" and ends with
> > "#"...
> >
> > Is there any way of using Regex to split it like the previous example??
> >
> > Thanks in advance
> >
Author
17 Oct 2006 8:38 PM
Jeff Dillon
The first solution is easier, and works

Dim sTest As String = "*One-*Two-*Three-"

sTest = sTest.Replace("*", "")

Dim s As String() = Regex.Split(sTest, "-")

Do your homework



Show quoteHide quote
"Jordi Rico" <jordir***@gmail.com> wrote in message
news:1161078451.253492.220250@h48g2000cwc.googlegroups.com...
> Thanks a lot Chris, in fact that's the solution I could find searching
> everywhere!
> I'm absolutely new in the world of Regex, and, although it's really
> hard, it is going to be very useful for our new project, as I have to
> parse a lot of diferent incoming messages... so now I'm going to study
> the basis of these methods...
>
>
> Chris ha escrito:
>
>> Hi Jordi,
>>
>> I know this isn't as straightforward as Regex.Split(), but it does
>> exactly
>> what you want.
>>
>> Begin code:
>> ===================================
>> imports Microsoft.VisualBasic
>> imports System
>> imports system.Text.RegularExpressions
>> imports System.Collections
>>
>> public module MyModule
>>  Sub Main()
>>  Dim myItems as New ArrayList
>>  myItems = getValues("*One#*Two#*Three#")
>>
>>   For each item as string in myItems
>>    Console.WriteLine( item & VbCrLf)
>>   next
>>
>>   Console.ReadLine()
>>  end sub
>>
>>     'Here is where the real work gets done
>>     Function getValues(ByVal Input As String) As ArrayList
>>         Dim RegexObj As String = "\*(?<ValueIwant>.+?)\#"
>>         Dim options As RegexOptions = RegexOptions.None
>>         Dim matches As MatchCollection = Regex.Matches(Input, RegexObj,
>> options)
>>         Dim myMatchArray As New ArrayList
>>
>>         For Each foundItem As Match In matches
>>             myMatchArray.Add(foundItem.Groups("ValueIwant").Value)
>>         Next
>>
>>         'You could skip the above loop and access
>>         'found items directly. For example:
>>         'matches.Item(i).Groups("ValueIwant").Value
>>         'Where i is an integer >= 0
>>
>>         Return myMatchArray
>>     End Function
>>
>> end module
>> ===================================
>> End Code.
>>
>> I don't think that using the Split function is the way to go here because
>> you have two possible characters to match. You could use an expression
>> like:
>> (\*\#|\*) but I don't know if that would confuse the Split function. You
>> can
>> try.
>>
>> I hope this helped.
>>
>> Chris
>>
>>
>> "Jordi Rico" <jordir***@gmail.com> wrote in message
>> news:1161005330.270951.227920@i42g2000cwa.googlegroups.com...
>> > Hi,
>> >
>> > I know I can split a string into an array doing this:
>> >
>> > Dim s As String()=Regex.Split("One-Two-Three","-")
>> >
>> > So I would have:
>> >
>> > s(0)="One"
>> > s(1)="Two"
>> > s(2)="Three"
>> >
>> > The problem is, I am receiving some kind of data this way:
>> >
>> > "*One#*Two#*Three#", it is, every word starts with "*" and ends with
>> > "#"...
>> >
>> > Is there any way of using Regex to split it like the previous example??
>> >
>> > Thanks in advance
>> >
>
Author
17 Oct 2006 9:54 PM
Branco Medeiros
Jordi Rico wrote (inline):
> I know I can split a string into an array doing this:
> Dim s As String()=Regex.Split("One-Two-Three","-")
>
> So I would have:
> s(0)="One"
> s(1)="Two"
> s(2)="Three"

You don't need a regex to such a split. Use the string:

    Dim S As String() = "One-Two-Three".Split("-"c)


> The problem is, I am receiving some kind of data this way:
> "*One#*Two#*Three#", it is, every word starts with "*" and ends with
> "#"...
> Is there any way of using Regex to split it like the previous example??

Again, the String itself might do it:

    Dim S As String() = "*One#*Two#*Three#".Split( _
      New Char() {"*"c, "#"c}, _
      System.StringSplitOptions.RemoveEmptyEntries)

HTH.

Regards,

Branco