regex?

Post your questions and help other users.

Moderator: Martin

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: regex?

Post by Desmanto » 28 Jul 2020 10:19

Sorry, I somehow missed one backslash at the regex for \D, should be \\D (double escape). Hence it is not working at your part.

Yours can be used too, but still have to fix the \D. Adding $ ensure it always match the last number. It should be working fine now. (I have corrected my previous post too).

Code: Select all

name = "xxxx1@gmail.com";

f = findAll(name, ".*?(\\d+)\\D*$", true);

if(!isEmpty(f))
{
  num = f[0][1] + 1; //increment num by 1
  r = max(length(f[0][1]), length(num)); //check if after added
  
  newnum = right("{num,numberformat,0000000000}", r); //reserve 10 digit and take right digit as many as r
  newname = replaceAll(name, "(.*?)(\\d+)(\\D*)$", "$1" + newnum + "$3"); //same regex pattern, but with new capture group and replacement
}
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply