473,473 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with regular expression

I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the separator
is enclosed in parentheses. For example, take the string "field1,
CONCAT(field2,' ', field3) as field23, field4". I would like to be able to
split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4

Thanks in advance.

--
Tony Marston
http://www.tonymarston.net
Jan 15 '06 #1
5 1624
Tony Marston wrote:
I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the separator
is enclosed in parentheses. For example, take the string "field1,
CONCAT(field2,' ', field3) as field23, field4". I would like to be able to
split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4


Can it happen that you will have nested parenthesis? :-)
Would you consider dumping preg_* function and parsing the string?

<?php
function parse_split_comma_ignore_paren($text) {
$retval = array();
$paren = 0;
$last_index = 0;
for ($index = 0; $index < strlen($text); ++$index) {
if ($text{$index} == '(') {++$paren; continue;}
/* assumes $paren will never be negative */
if ($text{$index} == ')') {--$paren; continue;}
if ($paren) continue;
if ($text{$index} == ',') {
$retval[] = trim(substr($text, $last_index, $index-$last_index));
$last_index = $index + 1;
}
}
$retval[] = trim(substr($text, $last_index));
return $retval;
}

$s = 'field1, CONCAT(field2, \' \', field3) as field23, field4,1,,3,4';

$parts = parse_split_comma_ignore_paren($s);
print_r($parts);
?>

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jan 15 '06 #2
Tony Marston wrote:
I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the separator
is enclosed in parentheses. For example, take the string "field1,
CONCAT(field2,' ', field3) as field23, field4". I would like to be able to
split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4

Thanks in advance.

--
Tony Marston
http://www.tonymarston.net


This works for the text format you described. Doesn't work in
situations where you might have paraphrases inside literal strings. For
that you'd need a solution like that one Pedro suggested.

preg_match_all('/\s*([^,]*\(.+[^\)]\)[^,]*|[^,]+)\s*/', $text, $m);
$list = $m[1];

Jan 16 '06 #3
([cC][oO][nN][cC][aA][tT]\s\(.*\)\s[aA][sS])?\s*(\w+\d+)|,([cC][oO][nN][cC][aA][tT]\s\(.*\)\s[aA][sS])?\s*(\w+\d+){1,}

the problem with doing this is that you are going to get CONCAT AS (...) as
one of your array elements, so just delete it or detect and ignore it.
you can always detect it with preg_match("/^CONCAT/",$string);
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:dq*******************@news.demon.co.uk...
I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the
separator is enclosed in parentheses. For example, take the string "field1,
CONCAT(field2,' ', field3) as field23, field4". I would like to be able to
split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4

Thanks in advance.

--
Tony Marston
http://www.tonymarston.net

Feb 9 '06 #4

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:lO********************@comcast.com...
([cC][oO][nN][cC][aA][tT]\s\(.*\)\s[aA][sS])?\s*(\w+\d+)|,([cC][oO][nN][cC][aA][tT]\s\(.*\)\s[aA][sS])?\s*(\w+\d+){1,}
try this one. you might get better results. the ?;, may need to be changed
to (, instead for things to work.
(?:[cC][oO][nN][cC][aA][tT]\s\(?:.*\)\s[aA][sS])?\s*(\w+\d+)(?:,(?:[cC][oO][nN][cC][aA][tT]\s\(?:.*\)\s[aA][sS])?\s*(\w+\d+)){0,}


the problem with doing this is that you are going to get CONCAT AS (...)
as one of your array elements, so just delete it or detect and ignore it.
you can always detect it with preg_match("/^CONCAT/",$string);
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:dq*******************@news.demon.co.uk...
I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the
separator is enclosed in parentheses. For example, take the string
"field1, CONCAT(field2,' ', field3) as field23, field4". I would like to
be able to split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4

Thanks in advance.

--
Tony Marston
http://www.tonymarston.net


Feb 15 '06 #5

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:K_********************@comcast.com...

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:lO********************@comcast.com...
([cC][oO][nN][cC][aA][tT]\s\(.*\)\s[aA][sS])?\s*(\w+\d+)|,([cC][oO][nN][cC][aA][tT]\s\(.*\)\s[aA][sS])?\s*(\w+\d+){1,}
try this one. you might get better results. the ?;, may need to be changed
to (, instead for things to work.
(?:[cC][oO][nN][cC][aA][tT]\s\(?:.*\)\s[aA][sS])?\s*(\w+\d+)(?:,(?:[cC][oO][nN][cC][aA][tT]\s\(?:.*\)\s[aA][sS])?\s*(\w+\d+)){0,}

I forgot to mention - this must be used with the preg_match series of
functions, not ereg series.


the problem with doing this is that you are going to get CONCAT AS (...)
as one of your array elements, so just delete it or detect and ignore it.
you can always detect it with preg_match("/^CONCAT/",$string);
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:dq*******************@news.demon.co.uk...
I am seeking help with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the
separator is enclosed in parentheses. For example, take the string
"field1, CONCAT(field2,' ', field3) as field23, field4". I would like to
be able to split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4

Thanks in advance.

--
Tony Marston
http://www.tonymarston.net



Feb 15 '06 #6

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

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
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...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
6
by: deepak_kamath_n | last post by:
Hello, I am relatively new to the world of regex and require some help in forming a regular expression to achieve the following: I have an input stream similar to: Slot: slot1 Description:...
14
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.