Advertisement

10.26.2007 at 03:11AM PDT, ID: 22919779
[x]
Attachment Details

Regular expression (Perl) / Preg_replace.  Replace all spaces except those inside quotes ???

Asked by R-U-Bn in Regular Expressions, PHP Scripting Language

Tags: perl, spaces, replace, regular, preg_replace

Hello, I want my preg_replace change all (multiple) spaces into one charachter | , EXCEPT those spaces that are inside quotes, but I can't find the right expression.  

Some examples:
testA testB testC testD            -->      testA|testB|testC|testD
testA "testB testC" testD      -->      testA|"testB testC"|testD
testA "testB testC testD"      -->      testA|"testB testC testD"
"testA testB testC" testD      -->      "testA testB testC"|testD

If possible also with the quotes gone, but I can do that with placing the quote in a second item of 2 arrays in preg_replace.

What I found so far is the following, but it's no good:
preg_replace ( array('/(?<![" ])\b(\s+)\b/','/\\\"/'), array('|','')

Help, I need somebody.  Help, not just anybody.  He-e-elp.  (Referring to the song ;-) )Start Free Trial
[+][-]10.26.2007 at 03:24AM PDT, ID: 20154355

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.26.2007 at 03:51AM PDT, ID: 20154445

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.26.2007 at 04:07AM PDT, ID: 20154518

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Regular Expressions, PHP Scripting Language
Tags: perl, spaces, replace, regular, preg_replace
Sign Up Now!
Solution Provided By: Roonaan
Participating Experts: 3
Solution Grade: A
 
 
[+][-]10.26.2007 at 05:47AM PDT, ID: 20154944

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.26.2007 at 05:50AM PDT, ID: 20154966

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906