Question

Is there any way to achieve the equivalent of a negative lookbehind in Dart regexp? I need to match a string that does not start and end with a specific set of characters.

I am unable to find a regex that does this without failing if the matched part is found at the beginning of the string. Negative lookbehinds seem to be the only answer, but Dart doesn't have one.

This is the regex that I would like to work, but it doesn't:

(?<!(Me\.)|[frFR])(text)(?!\w)

It would matched .text or text= but not Me.text or FtextABC

Was it helpful?

Solution 2

Your answer lies here:

javascript lookbehind mimic

The thing is that DART regex has exactly the same power as Javascript regex. Much to lamentation of many, Javascript does not support lookbehinds. But that link will provide an answer.

OTHER TIPS

For those who need lookbehind assertions in Dart RexExp, as of version 2.2.1-dev.1.0, Dart supports lookbehind assertions. So OP's RegExp works in Dart as it is written.

RegExp re = new RegExp(r"(?<!(Me\.)|[frFR])(text)(?!\w)");
List<String> tests = ['.text', 'text=', 'Me.text', 'FtextABC'];
for (String t in tests) {
  print('$t: ${re.hasMatch(t)}');
}

/*
prints: 
.text: true
text=: true
Me.text: false
FtextABC: false
*/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top