regex.split, can I keep the delimeter

C

Claud Balls

I am splitting large files based on a text delimeter, but I don't want
the delimeter left out of the string. For example if I had a string
"NAME: Bill TOWN: Helena NAME: Frank TOWN: Helena"
I would want to split on NAME: giving me

s(0) = NAME: Bill TOWN: Helena
s(1) = NAME: Frank TOWN: Helena
 
C

Cor Ligthert

Claud,

Why don't you just use the standard string split in stead of the in this
case only time consuming Regex.

And adding that name before afterwards is a piece of cake, that you can do
probably easy in the time you win by not using the Regex for this.

for each str as String in myarray
str = "NAME:" & str
next

I hope this helps,

Cor
 
C

Claud Balls

I used regex.split because it recognized the whole string as the
delimeter. String.split split on every character in the delimeter like
this:
N
A
M
E
: Bill TOW
N
: H
el
e
n
a

And I'll probably go with a stringbuilder to append the data, but I was
hoping there was a way around that.
 
J

JohnBoy

Give this a try:

Dim str As String = "NAME: Bill TOWN: Helena NAME: Frank TOWN: Helena"
Dim reg As New Regex("(\s?NAME:\s [a-z]+\s TOWN:\s [a-z]+\s?)",
RegexOptions.IgnoreCase)
Dim mtch As Match


For Each mtch In reg.Matches(str)
Debug.WriteLine(mtch.Value)
Next
 
J

JohnBoy

Exact code that I used:

Dim str As String = "NAME: Bill TOWN: Helena NAME: Frank TOWN: Helena" &
vbCrLf & "NAME: Bob TOWN: Helena"
Dim reg As New Regex("(?<From>\s?NAME:\s[a-z]+\sTOWN:\s[a-z]+)\s*",
RegexOptions.IgnoreCase)
Dim mtch As Match


For Each mtch In reg.Matches(str)
Debug.WriteLine(mtch.Groups("From").ToString)
Next
 
J

JohnBoy

Sorry, Claud. There should be no space after the three \s. I changed the
code and forgot to remove them.

'-- Spaces have been removed
"(\s?NAME:\s[a-z]+\sTOWN:\s[a-z]+\s?)"

You should use the updated one that I sent instead. It works better.

'-- Better
"(?<From>\s?NAME:\s[a-z]+\sTOWN:\s[a-z]+)\s*"
 
C

Claud Balls

Will Regex allow me to pull a chunk of data from between two variables?
For example, I read the following into a variable

A1 asdf asdf 1 324 25
B1 af 234 wt sdgf 5w4
C1 4q43g s 5 w343 4 4343
D1 24 gw 3 43 gt 4334 34 3dgf
E1 asfq2543 4 3t4 34434q3
F1 asdf233 q44t43rgg34 34
G1 aqt43rfd4343gfdf43 234

Is there a way to split or match so I only get this:
B1 af 234 wt sdgf 5w4
C1 4q43g s 5 w343 4 4343
D1 24 gw 3 43 gt 4334 34 3dgf
E1 asfq2543 4 3t4 34434q3
F1 asdf233 q44t43rgg34 34

by feeding a regular expression the B1 and F1 variables?
 
J

JohnBoy

I'm not quit sure what you mean but try this out and see if this is what you
want. This will only work if you have vbCrLf between your string items. Play
around with it.

Dim str As String = "A1 asdf asdf 1 324 25" & vbCrLf _
& "B1 af 234 wt sdgf 5w4" & vbCrLf _
& "C1 4q43g s 5 w343 4 4343" & vbCrLf _
& "D1 24 gw 3 43 gt 4334 34 3dgf" & vbCrLf _
& "E1 asfq2543 4 3t4 34434q3" & vbCrLf _
& "F1 asdf233 q44t43rgg34 34" & vbCrLf _
& "G1 aqt43rfd4343gfdf43 234+"




Dim reg As New Regex("(?<Item>[B-F]1( |\w*)*)")

Dim mtch As Match


For Each mtch In reg.Matches(str)
Debug.WriteLine(mtch.Groups("Item").ToString)
Next
 

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