I want to write four groups with only one regular expression.
I have this piece of text:
PAY VALUES TO SUM - WITHOU NOTHING: 0.00 (IGNORE THE REST)
PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 (IGNORE THE REST)
PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 (IGNORE THE REST)
PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 (IGNORE THE REST)
I want to get the numbers after the keys:
PAY VALUES TO SUM - WITHOU NOTHING:
PAY VALUES TO SUM - ADD. 50 YEARS:
PAY VALUES TO SUM - ADD. 70 YEARS:
PAY VALUES TO SUM - ADD. 80 YEARS:
I can use only one regex, so I tried this approach:
var text = 'PAY VALUES TO SUM - WITHOU NOTHING: 0.00 13º WORD WORD: 0.00' +
text += 'PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 WORD WORD WORD WORD WORD WORD WORD: 0.00';
var reg = new RegExp(SOME REGEX);
var match = reg.exec(text);
console.log(match[1], match[2], match[3], match[4]);
The result was 0.00 0.00 .00 .00.
This is the regex that I tried to use:
new RegExp('PAY VALUES TO SUM - WITHOU NOTHING:' + '\\b.*?(?:\\d\\S*\\s+){1}(\\d\\S*)(?:\\s?\\d\\S*\\s+){14}(\\d\\S*)(?:\\s?\\d\\S*\\s+){14}(\\d\\S*)(?:\\s?\\d\\S*\\s+){29}(\\d\\S*)(?:\\s?\\d\\S*\\s+){41}(\\d\\S*)');
The idea is to capture groups after words or numbers that I can convey.
But obviously this is not true. How can I achieve my goal?
Sometimes the numbers change. They are not always 0.00.
I need to explicitly pass the name of the word and capture group. Since the text is huge, I just showed part of it above.
I have this other regular expression that can do what I'm trying, but with a different text:
var text = 'PREV SOC 01 672 1.653.806,08 18.512,98 1.667.621,57 2.647,38 07 23 12.965,11 0,00 12.965,11 0,00'
var reg = new RegExp('PREV SOC 01' + '\\b.*?(?:\\d\\S*\\s+){4}(\\d\\S*)(?:\\s?\\d\\S*\\s+){5}(\\d\\S*)', 'i');
var match = reg.exec(text);
console.log(match[1]);
console.log(match[2]);
"PREV SOC 01" , 1.667.621,57, 0,00.
:
var text = 'PAY VALUES TO SUM - WITHOU NOTHING: 0.00 13º WORD WORD: 0.00' +
text += 'PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 WORD WORD WORD WORD WORD WORD WORD: 0.00';
'PAY VALUES TO SUM - WITHOUT NOTHING:' , , .