dimanche 6 mai 2018

Using Regex to remove leading/trailing whitespaces, except for quotes

I am trying to write a regular expression which recognises whitespaces from a user input string, except for between quotation marks ("..."). For example, if the user enters

     #load     "my   folder/my  files/    program.prog"     ;

I want my regex substitution to transform this into

#load "my   folder/my  files/    program.prog" ;

So far I've implemented the following (you can run it here).

#include <iostream> 
#include <string>
#include <regex>

int main(){
  // Variables for user input
  std::string input_line;
  std::string program;

  // User prompt
  std::cout << ">>> ";
  std::getline(std::cin, input_line);

  // Remove leading/trailing whitespaces
  input_line = std::regex_replace(input_line, std::regex("^ +| +$|( ) +"), "$1");

  // Check result
  std::cout << input_line << std::endl;

  return 0;
}

But this removes whitespaces between quotes too. Is there any way I can use regex to ignore spaces between quotes?

Aucun commentaire:

Enregistrer un commentaire