Y Borys
Y Borys

Reputation: 553

How to remove spaces from string except those in quotes

I have a string like this:

'Hello ' world  '. Hello world'

I need to remove spaces, but not inside quotes. For current string result should be:

Hello world. Hello world

Is any regular expression can help with this.
Thank You

Upvotes: 1

Views: 1045

Answers (2)

Alexander Farber
Alexander Farber

Reputation: 22978

Solution to your problem as a perl-oneliner (you can see how it removes whitespace around the 1st world):

# echo "'Hello ' world  '. Hello world'" | perl -ne "print \$1 while /('[^']+'|[^' ]+)/g"
'Hello 'world'. Hello world'

Use the regex /('[^']+'|[^' ]+)/g in your ActionScript program and you have it.

The key part of the regex is [^'] which means: match any character except single quote.

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 30995

You can use the following regex:

('.*?').*?\s*(\w+)\s*

Check the Substitution on this working demo

Upvotes: 1

Related Questions