Home > Software engineering >  Regex in Flutter to find double quotes enclosed words and escaped single quotes
Regex in Flutter to find double quotes enclosed words and escaped single quotes

Time:01-25

I am using this Regex in my Flutter App to find words enclosed by single-quotes that end with a .tr:

r"'[^'\\]*(?:\\.[^'\\]*)*'\s*\.tr\b"

Now I need another expression that is almost the same but looks for words enclosed by dobule-quotes, ending with .tr and might contain escaped single-quotes.

I tried simply changing the single quotes to double quotes from the first expression, but Flutter is giving me errors... I need to escaped some characters but I can not make it work. Any idea?

An edge case it should match is:

"Hello, I\'m Chris".tr

CodePudding user response:

you need to use \ before every " in your RegExp's source, try this:

RegExp regExp = new RegExp(r'\"[^\"\\]*(?:\\.[^\"\\]*)*\"\s*\.tr\b');

print("${regExp.hasMatch('"Hello, I\'m Chris".tr')}"); // result = true

CodePudding user response:

You may use this regex for double quoted text that can have any escaped character followed by .tr and word boundary:

r""""[^"\\]*(?:\\.[^"\\]*)*"\s*\.tr\b"""

RegEx Demo

  • Related