Capturing four numbers with one regex JavaScript

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:' , , .

+3
5

:

var 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:'
];

, . [0-9.]+ [0-9.], 0 9 . (period). , , 0.00 314.159.

, [0-9.]+ :

var regex = RegExp(keys[0] + '\\s+([0-9.]+).*?' +
                   keys[1] + '\\s+([0-9.]+).*?' +
                   keys[2] + '\\s+([0-9.]+).*?' +
                   keys[3] + '\\s+([0-9.]+).*?', 'gi');

, \\s+ . \\s*, .

.*? . 'gi' (g) (i).

. , .* .

text = text.replace(/\s/g, ' ');

, .

function print(s) {
  document.write(s + '<br />');
}

var 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:'
];

// Compile a regular expression that looks for a number after each key.
var regex = RegExp(keys[0] + '\\s+([0-9.]+).*?' +
                   keys[1] + '\\s+([0-9.]+).*?' +
                   keys[2] + '\\s+([0-9.]+).*?' +
                   keys[3] + '\\s+([0-9.]+).*?', 'gi');

var text = document.getElementById('content').innerHTML;

// Replace newlines and other invisible whitespace with visible spaces.
text = text.replace(/\s/g, ' ');

// Find and display all matches.
var count = 0;
while (true) {
  var match = regex.exec(text);
  if (match === null) {
    break;
  }
  ++count;
  print('match ' + count + ': ' +
        [match[1], match[2], match[3], match[4]].join(' '));
}
body {
  font-family: sans-serif;
}
#content {
  display: none;
}
<div id="content">
PAY VALUES TO SUM - WITHOU NOTHING: 0.00 13º WORD WORD: 0.00
PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 WORD WORD WORD: 0.00
PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 WORD WORD WORD: 0.00
PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 WORD WORD WORD WORD WORD WORD WORD: 0.00
</div>
0

, 0.00, Regex (\d.\d{2}) :

var text = 'PAY VALUES TO SUM - WITHOUT NOTHING: 0.13 13º WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 50 YEARS: 3.85 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 70 YEARS: 9.02 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 80 YEARS: 5.21 WORD WORD WORD WORD WORD WORD WORD: 0.00';

var re = /(\d.\d{2})/g;

var m;
 
while ((m = re.exec(text)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    document.write(m[0]+" ");
}

regex101 DEMO.

: document.write() , .

EDIT:

Regex (PAY VALUES TO SUM - (?:WITHOU NOTHING|ADD. \d{2} YEARS): (\d.\d{2})) , , , , :

var text = 'PAY VALUES TO SUM - WITHOU NOTHING: 0.13 13º WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 50 YEARS: 3.85 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 70 YEARS: 9.02 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 80 YEARS: 5.21 WORD WORD WORD WORD WORD WORD WORD: 0.00';

var re = /(PAY VALUES TO SUM - (?:WITHOU NOTHING|ADD. \d{2} YEARS): (\d.\d{2}))/g;

var m;

while ((m = re.exec(text)) !== null) {
  if (m.index === re.lastIndex) {
    re.lastIndex++;
  }
  document.write(m[0] + "<br>");
}
+1

:

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>');
+1

, 1- xx.xx : , .

, :

/^.*?:\s*([\d\.]+).*/gm

And this is a demo: https://regex101.com/r/gT9tJ0/1

0
source

Try the following: you can use this short regular expression:/PAY\s+VALUES\s+TO\s+SUM\s+[^:]+:\s*([\d.]+)/gmi

Regex Demo

    var re = /PAY\s+VALUES\s+TO\s+SUM\s+[^:]+:\s*([\d.]+)/gmi; 
    var str = 'var text = \'PAY VALUES TO SUM - WITHOU NOTHING: 0.00 13º WORD WORD: 0.00\' +\ntext += \'PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 WORD WORD WORD: 0.00\';\ntext += \'PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 WORD WORD WORD: 0.00\';\ntext += \'PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 WORD WORD WORD WORD WORD WORD WORD: 0.00\';\n';
    var m;
     
    while ((m = re.exec(str)) !== null) {
        if (m.index === re.lastIndex) {
            re.lastIndex++;
        }
        // View your result using the m-variable.
        document.write(m[1]+ ' ');
    }
Run code
0
source

All Articles