Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 9th July 2008, 23:22
tlsuess tlsuess is offline
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default REGEX expression to ignore commas in quotes

Hey all,

I am not a pro at REGEX but I'm trying to find a way to do a preg_split command and have it split a string by commas (,) but not while it's within single-quotes (').

So it would match this:
Code:
1
2
4
'test,like,this,see'
5
6
'resume,coding'
7
from this:
Code:
1,2,4,'test,like,this,see',5,6,'resume,coding',7
Does anyone know of a REGEX pattern I can use or something I can throw into a preg_match call?

Thanks,
Todd
Reply With Quote
Sponsored Links
  #2  
Old 11th July 2008, 09:22
burschik burschik is offline
Junior Member
 
Join Date: May 2008
Posts: 24
Thanks: 0
Thanked 2 Times in 2 Posts
Default

It would be a good idea to tell us what language you are using, since not all regular expression implementations are created equal and since that knowledge might enable us to point out an alternative solution that might be possible in one language and not in another.

I'm pretty sure, for example, that you can not do what you want to do with real regular expressions, although it might be possible with pcres. Since you seem to be using php, allow me to suggest the following alternative to your approach:

HTML Code:
$input = "1,2,4,'test,like,this,see',5,6,'resume,coding',7";
preg_match_all("/'[^']+'|[^,]+/", $input, $output);
foreach ($output[0] as $string) {
  echo $string . "\n";
}
Reply With Quote
  #3  
Old 11th July 2008, 09:35
tlsuess tlsuess is offline
Junior Member
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
 
Default

Wow it works! I was trying out different patterns but I think this one nailed it.

Thanks for the help and yes I am using PHP. I forgot that REGEX differs between programming languages (only by minor details).
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 22:52.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.