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

Need regular expression to parse string

I'm writing an app in vb.net 1.1 and I need to parse strings that look
similar to the one below. All 5 rows will make up one string. I have a
form where a use can copy/paste data like what you see below from excel,
word, notepad, etc.. into a textbox on my form. I need to break each line
into 2 numbers which I'll use as parameters for another function. in all
cases each line will be separated with a vbNewline and in most cases the 2
numbers in the line (2483 and 21 for example) will be separate be a tab
space, but I cant guarantee that. It should however be some sort of white
space in-between the 2 numbers. I could split the string below into 5
smaller strings, but I'm short on a reliable way to consistently parse the 2
numbers from each string.

2483 21
2484 23
24853 3
2486 14
2487 5

the end result of what I want to do is parse this string where I can feed
each value into parameters like this

param1=2483 param2=21
param1=2484 param2=23
param1=24853 param2=3
param1=2486 param2=14
param1=2487 param2=5
any good recommendations?

Thanks

--
mo*******@nospam.com
Nov 21 '05 #1
8 5007
Moondaddy,

It is known by most regulars in this newsgroup that I do not like the Regex.
Think about it that is at least (for simple problems) about 20 times slower
than a normal loop and replace function.

So I would use for your problem something as (not tested)
\\\
For each st as string in myrows
st = "param1=" & st
st = st.replace(" "," param2=")
next
///

I hope this helps?

Cor

"moondaddy"
I'm writing an app in vb.net 1.1 and I need to parse strings that look
similar to the one below. All 5 rows will make up one string. I have a
form where a use can copy/paste data like what you see below from excel,
word, notepad, etc.. into a textbox on my form. I need to break each line
into 2 numbers which I'll use as parameters for another function. in all
cases each line will be separated with a vbNewline and in most cases the 2
numbers in the line (2483 and 21 for example) will be separate be a tab
space, but I cant guarantee that. It should however be some sort of white
space in-between the 2 numbers. I could split the string below into 5
smaller strings, but I'm short on a reliable way to consistently parse the
2
numbers from each string.

2483 21
2484 23
24853 3
2486 14
2487 5

the end result of what I want to do is parse this string where I can feed
each value into parameters like this

param1=2483 param2=21
param1=2484 param2=23
param1=24853 param2=3
param1=2486 param2=14
param1=2487 param2=5
any good recommendations?

Thanks

--
mo*******@nospam.com

Nov 21 '05 #2
Why not just use the Split function? You could first split the text into
lines using vbNewLine as the delimiter, then process each line in a For Next
loop. For each line, do a split based on the space character. If you only get
one string back, then try it again with the tab character. Can't think of
anything else that would cause horizontal whitespace, so those two ought to
do it.

"moondaddy" wrote:
I'm writing an app in vb.net 1.1 and I need to parse strings that look
similar to the one below. All 5 rows will make up one string. I have a
form where a use can copy/paste data like what you see below from excel,
word, notepad, etc.. into a textbox on my form. I need to break each line
into 2 numbers which I'll use as parameters for another function. in all
cases each line will be separated with a vbNewline and in most cases the 2
numbers in the line (2483 and 21 for example) will be separate be a tab
space, but I cant guarantee that. It should however be some sort of white
space in-between the 2 numbers. I could split the string below into 5
smaller strings, but I'm short on a reliable way to consistently parse the 2
numbers from each string.

2483 21
2484 23
24853 3
2486 14
2487 5

the end result of what I want to do is parse this string where I can feed
each value into parameters like this

param1=2483 param2=21
param1=2484 param2=23
param1=24853 param2=3
param1=2486 param2=14
param1=2487 param2=5
any good recommendations?

Thanks

--
mo*******@nospam.com

Nov 21 '05 #3
I knew you had a 'thing' about Regular Expressions but I didn't know why.

Do you have any evidence for your fears or is it anecdotal and/or a matter
of perception?

In my experience, using Regular Expressions, (even for simple problems - and
believe me, my Regex stuff is simple), does not appear to add any
'significant' overhead to a given program.

When I talk about 'significant', I'm not talking about a millisecond here
and millisecond there, rather I am talking about a process taking several
seconds longer using one mechanism as opposed to another.

Interested to hear your views?
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eF**************@TK2MSFTNGP14.phx.gbl...
Moondaddy,

It is known by most regulars in this newsgroup that I do not like the Regex. Think about it that is at least (for simple problems) about 20 times slower than a normal loop and replace function.

So I would use for your problem something as (not tested)
\\\
For each st as string in myrows
st = "param1=" & st
st = st.replace(" "," param2=")
next
///

I hope this helps?

Cor

"moondaddy"
I'm writing an app in vb.net 1.1 and I need to parse strings that look
similar to the one below. All 5 rows will make up one string. I have a
form where a use can copy/paste data like what you see below from excel,
word, notepad, etc.. into a textbox on my form. I need to break each line into 2 numbers which I'll use as parameters for another function. in all cases each line will be separated with a vbNewline and in most cases the 2 numbers in the line (2483 and 21 for example) will be separate be a tab
space, but I cant guarantee that. It should however be some sort of white space in-between the 2 numbers. I could split the string below into 5
smaller strings, but I'm short on a reliable way to consistently parse the 2
numbers from each string.

2483 21
2484 23
24853 3
2486 14
2487 5

the end result of what I want to do is parse this string where I can feed each value into parameters like this

param1=2483 param2=21
param1=2484 param2=23
param1=24853 param2=3
param1=2486 param2=14
param1=2487 param2=5
any good recommendations?

Thanks

--
mo*******@nospam.com


Nov 21 '05 #4
Stephany,

A nice message from you. I have one thing against the regular expressions
and that is not the performance but the readability.

It needs for me (see "me") more time to use and reread the Regex than using
a good structured program language and the classes of dotNet. (I have the
same against SQL scripting by the way)

The benefit should be that in one time a lot of changing is done and
therefore fast. However, I did a lot of testing according too messages in
this newsgroup. (You can Google for it). The Regex in dotNet those test was
always the almost slowest solution. (Splitting strings is slower).

As you maybe have seen from me, I find performance in a program less
important than (afterwards) readable code.

However probably is the reason that I do not like it as well is because I
can do very much (without taking much time) with the String methods and
therefore never take time to look at Regex. (Although when I will have a
situation with variable changing keywords, I will for sure use the Regex to
make a filter with that).

About performance it was in all my test even more than 20 times slower, but
as your stated it goes (as far as I remember me now) about a few seconds
when it is about files from more than 1 Mb in a modern computer. I find that
not important because my experience of the behaviour from a user is that
when he is aware of such a situation takes a natural rest moment.

But I use that performance argument now more than the readability, it seems
that some people find the performance even the smallest more important than
that. (Look for discussions about unboxing which takes really for us humans
impossible to think about small amounts of time).

I hope I explained with this why I have something against the Regex

Cor

Nov 21 '05 #5
Leigh,

I just wrote a message to Stephanie, where I told the split was slow, I made
that before I readed your message, so please don't make a wrong conclussion.

In this case I think the Split can be a solution as well, it was my first
thought. However it will not hold less rows than the one I provided. The
performance for this simple things is not important in my opinion and so a
matter of taste which code to use.

Cor
Nov 21 '05 #6
Thanks for all the replies below. I ended up looping thru the data and
using the split function on the vbNewLine of the string. As you can see
below, the string has 4 or 5 vbNewLine(s). The string is parsed into an
array of 5 strings so I wrote some code to split the 2 parameters out of
each string by searching or spaces or vbTab, then trimming the data on each
side of the space or vbTab.

As for performance, its not an issue here since there might only be 20 line
items to parse out. But often I do data cleansing and one time I wrote
several regular expressions to help parse parameters out of 15000+ lines of
text and that took minutes to run, where as I wrote something similar in vba
years ago and took about 10 seconds to run through just as much data. So the
vba was more code to write and ran faster, but in the recent example, I
don't know if I could have written vb.net code to do the same kind of
sophisticated parsing regex did.

Thanks for the comments.

--
mo*******@nospam.com
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eF**************@TK2MSFTNGP14.phx.gbl...
Moondaddy,

It is known by most regulars in this newsgroup that I do not like the Regex. Think about it that is at least (for simple problems) about 20 times slower than a normal loop and replace function.

So I would use for your problem something as (not tested)
\\\
For each st as string in myrows
st = "param1=" & st
st = st.replace(" "," param2=")
next
///

I hope this helps?

Cor

"moondaddy"
I'm writing an app in vb.net 1.1 and I need to parse strings that look
similar to the one below. All 5 rows will make up one string. I have a
form where a use can copy/paste data like what you see below from excel,
word, notepad, etc.. into a textbox on my form. I need to break each line into 2 numbers which I'll use as parameters for another function. in all cases each line will be separated with a vbNewline and in most cases the 2 numbers in the line (2483 and 21 for example) will be separate be a tab
space, but I cant guarantee that. It should however be some sort of white space in-between the 2 numbers. I could split the string below into 5
smaller strings, but I'm short on a reliable way to consistently parse the 2
numbers from each string.

2483 21
2484 23
24853 3
2486 14
2487 5

the end result of what I want to do is parse this string where I can feed each value into parameters like this

param1=2483 param2=21
param1=2484 param2=23
param1=24853 param2=3
param1=2486 param2=14
param1=2487 param2=5
any good recommendations?

Thanks

--
mo*******@nospam.com


Nov 21 '05 #7
Hi,

VB.NET does support regex, you may try to take a look at the Regex Class in
the .net framework.
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemtextregularexpressionsregexclasstopic.a sp

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #8
OK Thanks!

--
mo*******@nospam.com
""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:tG**************@cpmsftngxa06.phx.gbl...
Hi,

VB.NET does support regex, you may try to take a look at the Regex Class in the .net framework.
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemtextregularexpressionsregexclasstopic.a sp

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #9

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

Similar topics

8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
4
by: Ben Dewey | last post by:
Hey, I have only been playing with regular expressions for some time. I am working on some code that parses and object 560 event log. I have created two expressions the first one which works...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: John Salerno | last post by:
Ok, this might look familiar. I'd like to use regular expressions to change this line: self.source += '<p>' + paragraph + '</p>\n\n' to read: self.source += '<p>%s</p>\n\n' % paragraph ...
5
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex =...
3
by: rupinderbatra | last post by:
Hello everyone, I am using a regular expression to parse a text string into various parts -- for ex: string "How do you do" will be changed to array with all the words and white spaces. I am...
0
by: ahropak | last post by:
Hi, I have a question regarding a regular expression within Regex.Split() method which will help me to break each line of code into tokens. I'm trying to parse some lines of C# source code and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.