473,320 Members | 2,080 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.

help with regex?

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.
Expand|Select|Wrap|Line Numbers
  1. Dim re As Object = SplitAdv(sr.ReadToEnd)
  2.  
  3. Function SplitAdv(ByVal strInput)
  4. Dim objRE
  5. Dim pattern As String
  6. pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
  7. objRE = New Regex(pattern)
  8. ' .Replace replaces the comma that we will use with
  9. ' chr(8), the \b character which is extremely unlikely
  10. ' to appear in any string it then splits the line into
  11. ' an array based on the \b
  12. 'Console.WriteLine(objRE.ToString)
  13. SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
  14.  
  15. End Function
  16.  

Dec 10 '07 #1
4 1248
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!

"maggie" wrote:
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.
Expand|Select|Wrap|Line Numbers
  1. Dim re As Object = SplitAdv(sr.ReadToEnd)
  2. Function SplitAdv(ByVal strInput)
  3.         Dim objRE
  4.         Dim pattern As String
  5.         pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
  6.         objRE = New Regex(pattern)
  7.         ' .Replace replaces the comma that we will use with
  8.         ' chr(8), the \b character which is extremely unlikely
  9.         ' to appear in any string it then splits the line into
  10.         ' an array based on the \b
  11.         'Console.WriteLine(objRE.ToString)
  12.         SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
  13.     End Function
  14.  

Dec 10 '07 #2
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" <ma****@discussions.microsoft.comwrote in message
news:25**********************************@microsof t.com...
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.
Expand|Select|Wrap|Line Numbers
  1. Dim re As Object = SplitAdv(sr.ReadToEnd)
  2. Function SplitAdv(ByVal strInput)
  3.        Dim objRE
  4.        Dim pattern As String
  5.        pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
  6.        objRE = New Regex(pattern)
  7.        ' .Replace replaces the comma that we will use with
  8.        ' chr(8), the \b character which is extremely unlikely
  9.        ' to appear in any string it then splits the line into
  10.        ' an array based on the \b
  11.        'Console.WriteLine(objRE.ToString)
  12.        SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
  13.    End Function
  14.  


Dec 10 '07 #3
I ,too,have a big headache, but you helped. I'm going to try it. Thanks very
much.
"eBob.com" wrote:
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" <ma****@discussions.microsoft.comwrote in message
news:25**********************************@microsof t.com...
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.
Expand|Select|Wrap|Line Numbers
  1.  Dim re As Object = SplitAdv(sr.ReadToEnd)
  2.  
  3.  Function SplitAdv(ByVal strInput)
  4.         Dim objRE
  5.         Dim pattern As String
  6.         pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
  7.         objRE = New Regex(pattern)
  8.         ' .Replace replaces the comma that we will use with
  9.         ' chr(8), the \b character which is extremely unlikely
  10.         ' to appear in any string it then splits the line into
  11.         ' an array based on the \b
  12.         'Console.WriteLine(objRE.ToString)
  13.         SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
  14.  
  15.     End Function
  16.  


Dec 10 '07 #4
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" wrote:
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" <ma****@discussions.microsoft.comwrote in message
news:25**********************************@microsof t.com...
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.
Expand|Select|Wrap|Line Numbers
  1.  Dim re As Object = SplitAdv(sr.ReadToEnd)
  2.  
  3.  Function SplitAdv(ByVal strInput)
  4.         Dim objRE
  5.         Dim pattern As String
  6.         pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
  7.         objRE = New Regex(pattern)
  8.         ' .Replace replaces the comma that we will use with
  9.         ' chr(8), the \b character which is extremely unlikely
  10.         ' to appear in any string it then splits the line into
  11.         ' an array based on the \b
  12.         'Console.WriteLine(objRE.ToString)
  13.         SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
  14.  
  15.     End Function
  16.  


Dec 10 '07 #5

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

Similar topics

6
by: Tony C | last post by:
I'm writing a python program which uses regular expressions, but I'm totally new to regexps. I've got Kuchling's "Regexp HOWTO", "Mastering Regular Expresions" by Oreilly, and have access to...
8
by: Bibe | last post by:
I've been trying to get this going for awhile now, and need help. I've done a regex object, and when I use IsMatch, it's behavior is quite weird. I am trying to use Regex to make sure that a...
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...
4
by: henrik | last post by:
Hi I have a regex question. I want to find all content of a <td class="someclass"> tag. This means the expression should include all other tags included between <td class="someclass"> and </td>....
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
2
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
3
by: =?Utf-8?B?TmF2ZWVu?= | last post by:
Not sure if this is the right forum for this question but couldn'd find another newsgroup. I am new to Regular expressions and would like help in deciding which pattern allows me to split a...
10
by: supercrossking | last post by:
I am trying to the values of string of text in the sample before. The ds are for digits and s is for string and string of text is for a string with more than one or two values. I am trying to use...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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.