I want to capture four groups with only one regex.
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 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 only use one regular expression, 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 was trying 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 digits that I can pass in.
But obviously this is not valid. 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 the capture groups. Because the text is huge, I only showed part of it above.
I have this other regex that can do what i'm trying, but with another 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]);
In this case I'm passing the word 'PREV SOC 01' and two capture groups, one to capture 1.667.621,57 and the other for 0,00.
I need the same in this other text:
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';
I want to pass 'PAY VALUES TO SUM - WITHOUT NOTHING:' and choose what i want after digits and numbers.
Here is my interpretation of your problem:
var text = ''
+ 'PAY VALUES TO SUM - WITHOU NOTHING: 0.00 (IGNORE THE REST)\n'
+ 'PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 (IGNORE THE REST)\n'
+ 'PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 (IGNORE THE REST)\n'
+ 'PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 (IGNORE THE REST)';
var re = /^PAY VALUES TO SUM - (?:WITHOU NOTHING|ADD\. \d+ YEARS): ([\d.]+).*$/gm;
document.write('<pre>' + text.replace(re, '$1').replace(/\n/g, ' ') + '</pre>');
// a little less specific
var re = /^PAY VALUES TO SUM - [^:]+: ([\d.]+).*$/gm;
document.write('<pre>' + text.replace(re, '$1').replace(/\n/g, ' ') + '</pre>');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With