473,413 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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

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

Oct 16 '06 #1
7 2208
How about
strIn = replace(strIn,"*","")
Dim s As String()=Regex.Split(strIn,"#")
--
Ron W
www.WorksRite.com
"Jordi Rico" <jo*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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

Oct 16 '06 #2
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" <jo*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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
Oct 16 '06 #3
Actually his solution works. You have ONE blank array entry at the end, but
you could safely ignore that one.

Jeff
"Jordi Rico" <jo*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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" <jo*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.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

Oct 16 '06 #4
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").Va lue)
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" <jo*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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

Oct 16 '06 #5
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").Va lue)
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" <jo*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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
Oct 17 '06 #6
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

"Jordi Rico" <jo*******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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").Va lue)
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" <jo*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.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

Oct 17 '06 #7

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

Oct 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
5
by: Bob | last post by:
I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
3
by: Michael D Murphy | last post by:
Hi, I would like to know how to use Regular Expressions to iteratively return and print the items between the colons in the following string to say the console.. Any help would be appreciated....
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
7
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text))...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.