Home > Software engineering >  Regex: Equivalent expression without using positive lookbehind
Regex: Equivalent expression without using positive lookbehind

Time:04-15

I need to split 4d6/2d6/1d6 matching /. However, in order to split it I need to have specifically \d preceding / and another \d|d succeeding /, so other expressions like 1d6 db/2 don't get split. I've come to the solution of using positive lookbehind, but I need my regex to be valid in IOS systems too. The solution I've come up with is /(?<=\d)\/(?=\d|d)/g. Is there a way to create an equivalent to this expression without using positive lookbehind?

CodePudding user response:

You can use an extracting, matching approach rather than splitting:

/(?:\/|^)(.*?\d)(?=\/\d|$)/g
/(?:\/|^)([\w\W]*?\d)(?=\/\d|$)/g

See the regex demo. Your value is in Group 1. The regex with [\w\W] matches across lines, too (as . does not match line breaks by default).

Details:

  • (?:\/|^) - a non-capturing group that matches either / or start of string
  • (.*?\d) - Group 1: any zero or more chars (here, other than line break chars) as few as possible, and then a digit
  • (?=\/\d|$) - a location that is immediately followed with / digit, or end of string.

In JavaScript, you can either use const matches = Array.from(text.matchAll(/(?:\/|^)(.*?\d)(?=\/\d|$)/g), x => x[1]), or - if you cannot use this syntax - a more verbose legacy extraction like

var s = "4d6/2d6/1d6";
var re = /(?:\/|^)(.*?\d)(?=\/\d|$)/g;
var matches=[], m;
while(m=re.exec(s)) {
  matches.push(m[1]);
}
console.log(matches)

  • Related