473,324 Members | 2,473 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,324 software developers and data experts.

Complex regular expression?

Hi All,

I couldn't find a regular expressions group to ask this in, so I
thought I'd ask here as I'm a little familiar with php's regular
expressions syntax.

I have a comma delimited text file that I need to change to being tab
delimited.

My problem is that commas appear in the values of one of my columns,
and I'm trying to think of a graceful way of changing the other commas
(ie those that do indicate the delimitation of a field, rather than
which appear within the value of a field) in the file to tabs without
affecting the commas that appear in the column in question.

An example of the contents of the file would be:

1,"1","20040301","08-08","BOOK, RETAIL",20.00,23.56
2,"1","20040301","03-09","BOOK, WHOLESALE, DISTRIBUTOR",15.99,22.00

So, I'm trying to create a regular expression that will change all the
commas to tabs, except where the comma(s) appear within quotes.

I've tried several different approaches, including a three-step
process where I just change the commas that appear within quotes to a
known 'escape' value, then changing all the commas to tabs, then
changing the 'escape' values back to commas, but I can't seem to
create a regular expression that will take into account the
possibility of several commas appearing between quotes.

I'm wondering if anyone can help me understand this better?

Many thanks in advance,

Murray
Jul 17 '05 #1
4 1978
"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:oa********************************@4ax.com...
Hi All,

I couldn't find a regular expressions group to ask this in, so I
thought I'd ask here as I'm a little familiar with php's regular
expressions syntax.

I have a comma delimited text file that I need to change to being tab
delimited.

My problem is that commas appear in the values of one of my columns,
and I'm trying to think of a graceful way of changing the other commas
(ie those that do indicate the delimitation of a field, rather than
which appear within the value of a field) in the file to tabs without
affecting the commas that appear in the column in question.

An example of the contents of the file would be:

1,"1","20040301","08-08","BOOK, RETAIL",20.00,23.56
2,"1","20040301","03-09","BOOK, WHOLESALE, DISTRIBUTOR",15.99,22.00

So, I'm trying to create a regular expression that will change all the
commas to tabs, except where the comma(s) appear within quotes.

I've tried several different approaches, including a three-step
process where I just change the commas that appear within quotes to a
known 'escape' value, then changing all the commas to tabs, then
changing the 'escape' values back to commas, but I can't seem to
create a regular expression that will take into account the
possibility of several commas appearing between quotes.


A not so elegant way:

function to_tab($matches) {
return strtr($matches[1], ",", "\t") . $matches[2];
}

$r = preg_replace_callback('/([^"]*)("?[^"]*"?)/', 'to_tab', $s);
Jul 17 '05 #2
M Wells schrieb:
Hi All,

I couldn't find a regular expressions group to ask this in, so I
thought I'd ask here as I'm a little familiar with php's regular
expressions syntax.

I have a comma delimited text file that I need to change to being tab
delimited.

My problem is that commas appear in the values of one of my columns,
and I'm trying to think of a graceful way of changing the other
commas (ie those that do indicate the delimitation of a field,
rather than which appear within the value of a field) in the file
to tabs without affecting the commas that appear in the column in
question.

An example of the contents of the file would be:

1,"1","20040301","08-08","BOOK, RETAIL",20.00,23.56
2,"1","20040301","03-09","BOOK, WHOLESALE, DISTRIBUTOR",15.99,22.00

So, I'm trying to create a regular expression that will change all
the commas to tabs, except where the comma(s) appear within quotes.

I've tried several different approaches, including a three-step
process where I just change the commas that appear within quotes to a
known 'escape' value, then changing all the commas to tabs, then
changing the 'escape' values back to commas, but I can't seem to
create a regular expression that will take into account the
possibility of several commas appearing between quotes.

I'm wondering if anyone can help me understand this better?

Many thanks in advance,

Murray


Another way to solve this problem is to replace all commas which where
NOT followed by spaces... If u can be sure that commas in quotes always
have a space behind them...

$new_string = preg_replace('/\,([\S])/',"\t$1",$string);

*Hannes*

Jul 17 '05 #3
M Wells wrote:
I have a comma delimited text file that I need to change to being tab
delimited.

My problem is that commas appear in the values of one of my columns,
and I'm trying to think of a graceful way of changing the other commas
(ie those that do indicate the delimitation of a field, rather than
which appear within the value of a field) in the file to tabs without
affecting the commas that appear in the column in question.


http://www.php.net/manual/en/function.fgetcsv.php

If you have commas inside the quoted fields this function takes care of it
for you. You can specify what sort of delimiter as well (eg tab, comma etc)

Chris

--
Chris Hope
The Electric Toolbox Ltd
http://www.electrictoolbox.com/
Jul 17 '05 #4

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:oa********************************@4ax.com...
Hi All,

I couldn't find a regular expressions group to ask this in, so I
thought I'd ask here as I'm a little familiar with php's regular
expressions syntax.

I have a comma delimited text file that I need to change to being tab
delimited.

My problem is that commas appear in the values of one of my columns,
and I'm trying to think of a graceful way of changing the other commas
(ie those that do indicate the delimitation of a field, rather than
which appear within the value of a field) in the file to tabs without
affecting the commas that appear in the column in question.

An example of the contents of the file would be:

1,"1","20040301","08-08","BOOK, RETAIL",20.00,23.56
2,"1","20040301","03-09","BOOK, WHOLESALE, DISTRIBUTOR",15.99,22.00

So, I'm trying to create a regular expression that will change all the
commas to tabs, except where the comma(s) appear within quotes.

I've tried several different approaches, including a three-step
process where I just change the commas that appear within quotes to a
known 'escape' value, then changing all the commas to tabs, then
changing the 'escape' values back to commas, but I can't seem to
create a regular expression that will take into account the
possibility of several commas appearing between quotes.

I'm wondering if anyone can help me understand this better?

Many thanks in advance,

Murray


Had a bit of a tinker, came up with this:

<?php
$x='1,2,3,"some text in quotes",4,5,"some more, this time with a comma"';
preg_match_all('/(".*?")/',$x,$r);

$r[0] now looks like this:
Array
(
[0] => "some text in quotes"
[1] => "some more, this time with a comma"
)

As you can see, the non-greediness of the regexp handles is the key. Run
your original line through substr_replace() to get these strings replaced
with tokens and resume where you left off.

HTH
Garp

Jul 17 '05 #5

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

Similar topics

4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
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...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.