473,320 Members | 1,572 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,320 software developers and data experts.

regex.split, can I keep the delimeter

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
10 1497
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
Nov 21 '05 #2
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3
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

"Claud Balls" <Li**@trainer.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #4
Didn't return any matches.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5
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

"Claud Balls" <Li**@trainer.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...
Didn't return any matches.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #6
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*"
Claud Balls" <Li**@trainer.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...
Didn't return any matches.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #7
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?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #8
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


"Claud Balls" <Li**@trainer.com> wrote in message
news:Oj**************@TK2MSFTNGP14.phx.gbl...
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?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #9
What do the <Item> and <From> tags do?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #10
It's just a name for the captured group. You can name it anything you like.

"Claud Balls" <Li**@trainer.com> wrote in message
news:e2*************@TK2MSFTNGP15.phx.gbl...
What do the <Item> and <From> tags do?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #11

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

Similar topics

4
by: Masahiro Ito | last post by:
I have attached a block of text similar to the type that I am working with. I have been learning a lot about Regex - it is quite impressive. I can easily capture bits of info, but I keep having...
1
by: Ada | last post by:
i'm trying to use Regex to match a 4 number group pattern. once a match is found, write it to RichTextBox in red. test data: This is my 4567 test data. 1234this is another line of data. Here's...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
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;}")...
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...
6
by: Jake Barnes | last post by:
This function has always worked for me just fine: function nl2br_js(myString){ // 02-18-06 - this function imitates the PHP command nl2br, which finds newlines in a string // and replaces them...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.