I am reading data from csv file, where Iam able to read the data even data has comma(,) in it using regex below, but unable to include time in that pattern ? can someone suggest how to match that.
Regex : (".*?"|[^",\s] )(?=\s*,|\s*$)
I need to include 2:00:00 AM as well
CodePudding user response:
We can try the following regex match all approach in JavaScript:
var input = '"23,42",ewtwe, sf,asf,2:00:00 AM,3;8;,3;8;';
var items = input.match(/".*?"|[^,\s] (?:\s [^,\s] )*/g);
console.log(items);
The regex pattern used here says to match:
".*?"a doubly quoted term|OR[^,\s]a non whitespace term excluding commas(?:\s [^,\s] )*optionally followed by whitespace and more similar terms
Note that we search eagerly first for doubly quoted terms, and only that failing do we use comma as a delimiter.
CodePudding user response:
We can add this part (?: \w )? after [^",\s] .
[^",\s] match one or more character except ", , and \s.
(?: \w )? followed by an optional (space and \w one or more word character). the (?: is the opening of the non-capturing group and the last ) is the closing bracket of it. The ? after the closing bracket makes the whole group optional.
(".*?"|[^",\s] (?: \w )?)(?=\s*,|\s*$)
See regex demo

