help with regex?

M

maggie

hi,
I need some help with a reg. expression. I have a comma delimited file with
quotes. Not every field has quotes, only some. This is a sample of my file:
99,"01/01/2007","23,000",1,34,"henry",132,"45.00"

I used some code from an article that I though would do what I needed, but
it splits my amount fields(76,000 into two different fields : 76, and 000...
Do I change my pattern ? I never used regex before and need some help. thank
you.
Code:
Dim re As Object = SplitAdv(sr.ReadToEnd)

Function SplitAdv(ByVal strInput)
Dim objRE
Dim pattern As String
pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
objRE = New Regex(pattern)
' .Replace replaces the comma that we will use with
' chr(8), the \b character which is extremely unlikely
' to appear in any string it then splits the line into
' an array based on the \b
'Console.WriteLine(objRE.ToString)
SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")

End Function
 
M

maggie

Hi,
I think I found a better pattern, but I can't seem to get it defined
correctly in a string
I have this:
pattern = "((?:[^',]|(?:'(?:\\{2}|'|[^'])*?'))*)"

I would like to change the single quotes to double quotes, but it doesnt
like what I'm doing. I tried putting escape characters before the " like so:
\", but it does not like it. How in the world can I do this? I've spent about
2 hours on this alone and am stuck. Please advise. Thanks!
 
E

eBob.com

You might find Expresso useful (http://www.ultrapico.com/).

Since I already have a headache from my own regex mystery today I did not
try to understand your pattern, and I have never used the Regex Split
method. But I did play around with your basic problem and came up with the
following pattern

((?<q>"?)[/,\w\d\.]+\k<q>,?)

which yields this result

99,
"01/01/2007",
"23,000",
1,34,
"henry",
132,
"45.00"

when fed your sample data. IF that's what you want, and IF you don't need
to use the Split method.

Good Luck, Bob
 
M

maggie

I ,too,have a big headache, but you helped. I'm going to try it. Thanks very
much.


eBob.com said:
You might find Expresso useful (http://www.ultrapico.com/).

Since I already have a headache from my own regex mystery today I did not
try to understand your pattern, and I have never used the Regex Split
method. But I did play around with your basic problem and came up with the
following pattern

((?<q>"?)[/,\w\d\.]+\k<q>,?)

which yields this result

99,
"01/01/2007",
"23,000",
1,34,
"henry",
132,
"45.00"

when fed your sample data. IF that's what you want, and IF you don't need
to use the Split method.

Good Luck, Bob


maggie said:
hi,
I need some help with a reg. expression. I have a comma delimited file
with
quotes. Not every field has quotes, only some. This is a sample of my
file:
99,"01/01/2007","23,000",1,34,"henry",132,"45.00"

I used some code from an article that I though would do what I needed, but
it splits my amount fields(76,000 into two different fields : 76, and
000...
Do I change my pattern ? I never used regex before and need some help.
thank
you.
Code:
Dim re As Object = SplitAdv(sr.ReadToEnd)

Function SplitAdv(ByVal strInput)
Dim objRE
Dim pattern As String
pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
objRE = New Regex(pattern)
' .Replace replaces the comma that we will use with
' chr(8), the \b character which is extremely unlikely
' to appear in any string it then splits the line into
' an array based on the \b
'Console.WriteLine(objRE.ToString)
SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")

End Function
 
M

maggie

eBob,

The dollar amounts came out on their own, but I still have some all strung
together with commas, which is better than what I had. I'll see if i can
separate them. Thanks again.

eBob.com said:
You might find Expresso useful (http://www.ultrapico.com/).

Since I already have a headache from my own regex mystery today I did not
try to understand your pattern, and I have never used the Regex Split
method. But I did play around with your basic problem and came up with the
following pattern

((?<q>"?)[/,\w\d\.]+\k<q>,?)

which yields this result

99,
"01/01/2007",
"23,000",
1,34,
"henry",
132,
"45.00"

when fed your sample data. IF that's what you want, and IF you don't need
to use the Split method.

Good Luck, Bob


maggie said:
hi,
I need some help with a reg. expression. I have a comma delimited file
with
quotes. Not every field has quotes, only some. This is a sample of my
file:
99,"01/01/2007","23,000",1,34,"henry",132,"45.00"

I used some code from an article that I though would do what I needed, but
it splits my amount fields(76,000 into two different fields : 76, and
000...
Do I change my pattern ? I never used regex before and need some help.
thank
you.
Code:
Dim re As Object = SplitAdv(sr.ReadToEnd)

Function SplitAdv(ByVal strInput)
Dim objRE
Dim pattern As String
pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
objRE = New Regex(pattern)
' .Replace replaces the comma that we will use with
' chr(8), the \b character which is extremely unlikely
' to appear in any string it then splits the line into
' an array based on the \b
'Console.WriteLine(objRE.ToString)
SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")

End Function
 

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