Struggling getting a working regex that doesn't capture quotes which are optional.
Code and jsfiddle below.
https://jsfiddle.net/zx7Lurob/1/
var txt = `#VER B 2000 20190403 "Text" 20190413`; //without quote 2000
var txt2 = `#VER B "2000" 20190403 "Text" 20190413`; //with qutoe 2000
var myRegexp = /(#VER)\s+"(.+)"|(.+)\s+(.+?)\s+(\d{8})\s+"(.+)"/g;
do {
var match = myRegexp.exec(txt2);
if (match != null)
{
match.forEach(function(value) {
console.log(value);
});
//console.log(match);
}
} while (match != null);
Gives a result with quoted values for txt2.
You can use
var myRegexp = /(#VER.*?)\s+(?:"([^"]*)"|(\S+))\s+(\d{8})\s+"([^"]*)"/g;
See the regex demo. Details:
(#VER.*?) - Group 1: #VER and then any zero or more chars, other than line break chars, as few as possible\s+ - one or more whitespaces(?:"([^"]*)"|(\S+)) - either ", then any zero or more chars other than " (captured into Group 2), or one or more non-whitespace chars (captured into Group 3),\s+ - one or more whitespaces(\d{8}) - Group 4: eight digits\s+ - one or more whitespaces"([^"]*)" - ", then any zero or more chars other than " (captured into Group 5), and then a " char.See the JavaScript demo:
var txt = `#VER B 2000 20190403 "Text" 20190413`;
var txt2 = `#VER B "2000" 20190403 "Text" 20190413`;
var myRegexp = /(#VER.*?)\s+(?:"([^"]*)"|(\S+))\s+(\d{8})\s+"([^"]*)"/g;
for (var txt of [txt, txt2]) {
do {
var match = myRegexp.exec(txt);
if (match != null)
{
console.log(txt, '=>',
[match[1], match[3] || match[2], match[4], match[5]]);
}
} while (match != null);
}
Its difficult to determine if a positional way points are intended or
a field delimiter.
Its my guess its actually a hybrid of both.
In that case a simple matching an optional quote on both sides of a
field and outside of the capture should do the trick.
/(\#VER.*)[ \t]+"?(.+?)"?[ \t]+(\d{8})[ \t]+"(.+)"/
https://jsfiddle.net/Lj4rzo2s/
( \#VER .* ) # (1)
[ \t]+
"?
( .+? ) # (2)
"?
[ \t]+
( \d{8} ) # (3)
[ \t]+
"
( .+ ) # (4)
"