Using regex to replace all NOT spaces in quotes in Ruby

I am trying to write a regex to replace all spaces that are not enclosed in quotation marks, something like this:

a = 4, b = 2, c = "space here" 

will return this:

 a=4,b=2,c="space here" 

I spent some time looking for this site, and I found a similar q / a ( Separate a string with spaces - keeping quoted substrings - in Python ), which replace all the spaces inside the quotes with a token that could be replaced again after wiping all the other spaces. .. but I was hoping there was a cleaner way to do this.

+4
source share
4 answers

It is worth noting that any solution to a regular expression will not be executed in the following cases:

 a = 4, b = 2, c = "space" here" 

Although it is true that you could create a regular expression to handle a case with three quotes, you cannot solve the problem in a general sense. This is a mathematically provable limitation of simple DFAs , of which regular expressions are direct representations. To perform any serious brackets / quotes comparisons, you will need a more powerful pushdown automaton , usually in the form of a text analyzer library (ANTLR, Bison, Parsec).

With that said, it sounds like regular expressions should be sufficient for your needs. Just keep in mind the limitations.

+8
source

It works:

 result = string.gsub(/( |(".*?"))/, "\\2") 
+4
source

I find this very clean:

 mystring.scan(/((".*?")|([^ ]))/).map { |x| x[0] }.join 

I doubt that gsub can do better (assuming you want to use a clean regular approach).

+2
source

try this one, the line in single / double loop is also mapped (so you need to filter them if you only need space):

 /( |("([^"\\]|\\.)*")|('([^'\\]|\\.)*'))/ 
0
source

Source: https://habr.com/ru/post/887827/


All Articles