I have data like this (some of the fields have values, some of them don't)
J;555;3;1990-02-19;1234;S;2020-10-17;;;;;value;3;ANOTHERVALUE
I need to add a new field right before the 5th semicolon, like this (the F value):
J;555;3;1990-02-19;1234;F;S;2020-10-17;;;;;value;3;ANOTHERVALUE
So far I have this:
^(([A-Z]);([0-9A-Z] );([?:0-9]);([0-9\-] );([A-Z0-9] ));(.*)$
And as replacement:
$1;$2;F;$7
As far as I know the ?: before is to mark optional, but in my case I need to represent empty fields between every semicolon
How can I solve this?
CodePudding user response:
const regex = /^([A-Z]);([0-9]*);([0-9]*);([0-9-]*);([0-9]*);(.*)$/
let str = 'J;555;3;1990-02-19;1234;S;2020-10-17;;;;;value;3;ANOTHERVALUE'
str = str.replace(regex, '$1;$2;$3;$4;$5;F;$6')
let expected = 'J;555;3;1990-02-19;1234;F;S;2020-10-17;;;;;value;3;ANOTHERVALUE'
console.log(str === expected)
str = 'J;;3;1990-02-19;;S;2020-10-17;;;;;value;3;ANOTHERVALUE'
str = str.replace(regex, '$1;$2;$3;$4;$5;F;$6')
expected = 'J;;3;1990-02-19;;F;S;2020-10-17;;;;;value;3;ANOTHERVALUE'
console.log(str === expected)
You can find an explanation on Regex101
Basically, I made use of *
which matches from zero to unlimited times to account for instances with missing values.
^([A-Z]);([0-9]*);([0-9]*);([0-9-]*);([0-9]*);(.*)$
Description
([A-Z])
Match anyA-Z
character;
Matches a semi-colon literally([0-9]*)
Match any number between zero and unlimited times;
Matches a semi-colon literally([0-9]*)
Match any number between zero and unlimited times;
Matches a semi-colon literally([0-9]*)
Match any number between zero and unlimited times;
Matches a semi-colon literally([0-9]*)
Match any number between zero and unlimited times;
Matches a semi-colon literally(.*)
Matches any character between zero and unlimited times
CodePudding user response:
You may use this code in notepad ,
Find:^((.*?;){5})
Replace with:$1F;