-2

I have a list of numbers

1.01.01.01

1.01.01.02

1.01.02.03

I need to add 1 to the number after the 2nd occurance of '.0':

1.01.02.01

1.01.02.02

1.01.03.03

I am using javascript. I have tried a few things, but I just get so confused with regex haha.


I have been playing, and split might be the way to go here, thanks Richard. Anyone happen to know increment +1 on a 01 in a string, or will I need to break down the string and turn it to an integer. first?

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Brendonius
  • 109
  • 3
  • 2
    Please show your attempted regex – anubhava Nov 15 '21 at 05:09
  • 3
    https://regex101.com/ is your friend, but performing additions with regex is probably painful. Why not split on the `.`, increment, and then join the strings? – Richard Nov 15 '21 at 05:10
  • Cheers guys, I have been playing, and split might be the way to go here, thanks Richard. Anyone happen to know increment +1 on a 01 in a string, or will I need to break down the string and turn it to an integer first? – Brendonius Nov 15 '21 at 05:22
  • What if the string were `1.01.09.03`? `1.00.90.03`? I don't see how a regular expression could be used to advantage here, as it is easy to do in code that does not employ a regex. – Cary Swoveland Nov 15 '21 at 07:30
  • 1
    @Brendonius: I have added your comment into your answer. It is always better to show your attempt or struggles in the question so that we understand your problem better and provide better guidance. – anubhava Nov 15 '21 at 13:00
  • 1
    cheers, yep - worked perfectly with a split. My brain was in a knot and had tried so many different regex patterns, didnt even think of it haha. – Brendonius Nov 15 '21 at 23:07
  • @Brendonius ... Regarding the provided approaches / solutions are there any questions left? – Peter Seliger Nov 17 '21 at 17:29
  • 1
    @Brendonius ... At SO it is considered to be a nice gesture from the one who got help, to provide some feedback and/or vote on answers and/or accept the answer which was the most helpful in solving the OP's problem. – Peter Seliger Nov 18 '21 at 18:27

2 Answers2

0

You may use this code snippet that does the job in these steps:

  1. Splits input string by .0
  2. Converts 2nd element to Integer and increments it by 1
  3. Stores resulting elements in an array ret
  4. Joins elements of ret with .0 again and saves new String in result array
  5. Finally displays result array

Code:

const arr = ['1.01.01.01','1.01.01.02', '1.01.02.03'];
const delim = '.0';
var result = [];

arr.forEach(str => {
  ret = [];
  str.split(delim).forEach((el, i) => 
     ret.push(i==2 ? parseInt(el)+1 : el)
  );
  result.push(ret.join(delim));
});

console.log(result);
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

A split and/but non regex based approach can be implemented as straightforward as the following code (of cause having no control over the validity of the parsed 3rd argument which is assumed to be a digit (sequence)) ...

const sampleData = [
  '1.01.01.02',
  '1.01.02.03',

  '1.01.0x.04',
  '1.01.0x.0x',

  '1.01.001.01.07.08.09',
  '1.01.013.03.01200230',
];

console.log(
  sampleData, '=>',

  sampleData.map(item => {
    const [leadingA, leadingB, digits, ...trailing] = item.split('.0');
    return [
      leadingA, leadingB, (parseInt(digits, 10) + 1), ...trailing
    ].join('.0');
  })
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

A more precise (edge cases) replace based approach is in need of a more complex capturing regex like ... /^((?:.*?\.0){2})(\d+)(.*)/ ... and a replacer function as its second argument.

The regex ... /^((?:.*?\.0){2})(\d+)(.*)/ ... reads like this ...

  1. Part 1: ^( (?:.*?\.0){2} )

    • From line start ^ capture ( ... ) the following match ...
    • (?: ... ){2} ... twice {2} a not captured ?: group ( ... ) of following pattern ...
    • .*?0 ... anything .* in a non ? greedy way until a Dot-Zero sequence .0 occurs.
  2. Part 2: (\d+)

    • Capture ( ... ) at least one + digit \d.
  3. Part 3: (.*)

    • Capture ( ... ) anything .* greedy until the end of the string.

The related replacer function is the callback of replace and gets passed as arguments the entire match (1st argument) followed by each captured value as separate argument, starting with the first and ending with the last capture.

Thus for the given example the replacer function's argument names (which can be freely chosen) and precedence are ... (match, leading, digits, trailing) ... where digits is the 2nd capture group, the digit (sequence) which the OP wants to become an incremented integer.

A solution then might look similar to the next provided example code ...

const sampleData = [
  '1.01.01.02',
  '1.01.02.03',

  '1.01.0x.04',
  '1.01.0x.0x',

  '1.01.001.01.07.08.09',
  '1.01.013.03.01200230',
];
// see ... [https://regex101.com/r/60omn1/2]
const regXCapture = (/^((?:.*?\.0){2})(\d+)(.*)/);

console.log(
  sampleData, '=>',

  sampleData.map(item =>
    item.replace(
      regXCapture,
      (match, leading, digits, trailing) => [
        leading,
        parseInt(digits, 10) + 1,
        trailing,
      ].join('')
    )
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37