0

I need to select all the whitespaces except those ones inside double quotes " "

So for the input below, I need to select all the whitespaces except those ones inside "constant one two" - consider that this code sample is my whole input string:

function void test (  )   {   
    if  ( false )   { 
        let s  =  "constant one two" ; 
        let s  =  null ; 
        let a [ 1 ]   =  a [ 2 ]  ; 
     } 
 } 

I tried a lot of options with positive and negative lookahead but with no luck yet.

halfer
  • 19,824
  • 17
  • 99
  • 186
Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
  • 1
    This is derived from the proposed duplicate: https://regex101.com/r/hfaHWN/1 – Nick May 31 '20 at 09:12
  • I believe these type of questions are asked with minor changes too frequently. Please see [this.](https://stackoverflow.com/questions/62105411/regex-for-splitting-a-string-while-ignoring-the-brackets/62106065#62106065) –  May 31 '20 at 14:00
  • What does " select all the whitespaces" mean? In what fashion do you want them and what do you want to do with them? To me you have asked half of a question, i.e. you have hinted at your real question but not said enough. – AdrianHHH May 31 '20 at 16:41
  • Why doesn't this answer your question? https://stackoverflow.com/questions/632475/regex-to-pick-characters-outside-of-pair-of-quotes – Braiam May 31 '20 at 19:48
  • @Nick thank you a lot that one worked. – Aliaksandr Sushkevich May 31 '20 at 19:58
  • @AdrianHHH I just wanted to split that string by whitespaces not including those ones inside double quotes. – Aliaksandr Sushkevich May 31 '20 at 20:04
  • You've mentioned strings inside double quotes, but in the code the double quotes are delimiters, not part of the string. Can you clarify what you mean here? – halfer May 31 '20 at 20:24
  • Selecting all the spaces is quite different to splitting by whitespaces. Please [edit] the question to explain what you are really trying to do and what you expect as the result. – AdrianHHH May 31 '20 at 20:37
  • @halfer Probably that explanation was a bit confusing by string I meant all that chunk of code I gave as an example. I need to split all that code by whitespaces excluding whitespaces inside `" "` – Aliaksandr Sushkevich May 31 '20 at 20:49
  • @AdrianHHH could you elaborate more on your statement? How's it different? I need a regex that will select correct whitespaces and then I just use it inside a split function. – Aliaksandr Sushkevich May 31 '20 at 20:57
  • That is the sort of thing you need to say, but in more detail, when you [edit] the question. Your question title says "grab all the spaces except ..." but your most recent comment is about splitting on spaces. As I am writing for the third time, please elaborate of exactly what you are trying to do. Please [edit] the question and explain in full what you are trying to do. – AdrianHHH May 31 '20 at 21:07
  • @AdrianHHH I’m just trying to understand how’s that relevant. Because in that case it doesn’t. if I can grab the correct whitespaces I can do the splitting on my own. – Aliaksandr Sushkevich May 31 '20 at 21:18
  • I am just trying to understand your poorly written question and help you to write one that can be understood. If you do not want to explain the problem then how do you expect people to provide you an answer. Nowhere in you question do you say what you want to do with the whitespaces and, as I asked in my first comment, "what do you want to do with them". – AdrianHHH May 31 '20 at 21:38
  • This is your question, you have the problem and so you need to put some effort into providing enough information (in the question itself) to allow someone to provide an answer. Until you [edit] the question to provide that missing information I will be unable to provide any further advice. – AdrianHHH May 31 '20 at 21:38

1 Answers1

1

Use

\s(?=([^"]|"[^"]*")*$)

See proof. It matches any whitespace followed with zero or more characters other than double quote or a substring between double quotes.

Or, use PCRE pattern if possible:

"[^"]*"(*SKIP)(*F)|\s

See another proof. It matches any substring between quotes and drops the match, else, it matches whitespace, which will be outside of the quotes.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37