Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to select all whitespace that IS in quotes?

For example:

string text = 'some text "and some more" and some other "this is a second group" okay the end';.

I'd like to capture all the spaces that are between quotation marks. The end goal is to replace those spaces with commas.

The end goal, for example:

'some text "and,some,more" and some other "this,is,a,second,group" okay the end'

For example, this would do what I'd like in javascript:

text.replace(/(["]).*?\1/gm, function ($0) {
    return $0.replace(/\s/g, ',');
});

Unfortunately, the only tool I have available is the find/replace feature of textmate.

I've found another that does the opposite of what I need, but uses one line like I need:

text.replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/gm, ',');

Thanks!

like image 965
Klnh13 Avatar asked Apr 18 '16 22:04

Klnh13


People also ask

What is the regex for white space?

The most common regex character to find whitespaces are \s and \s+ . The difference between these regex characters is that \s represents a single whitespace character while \s+ represents multiple whitespaces in a string.

How do you restrict whitespace in regex?

You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace.

Do I need to escape quotes in regex?

In order to use a literal ^ at the start or a literal $ at the end of a regex, the character must be escaped. Some flavors only use ^ and $ as metacharacters when they are at the start or end of the regex respectively. In those flavors, no additional escaping is necessary. It's usually just best to escape them anyway.

Can you use quotes in regex?

Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.


Video Answer


1 Answers

You can use

\s+(?=(?:(?:[^"]*"){2})*[^"]*"[^"]*$)

See the regex demo

The \s+ matches 1 or more whitespace that is followed by an odd number of double quotes.

Details: The whitespace matching part is simple, the positive lookahead requires

  • (?:(?:[^"]*"){2})* - zero or more sequences of 2 sequences matching 0+ characters other than a " and then a " (0+ "..."s)
  • [^"]*"[^"]* - 0+ characters other than a " followed with a " and again followed with 0+ characters other than a " (an odd quote must be to the right of the currently matched whitespace)
  • $ - end of string.
like image 151
Wiktor Stribiżew Avatar answered Oct 12 '22 21:10

Wiktor Stribiżew