简体   繁体   中英

Regex in Flutter to find double quotes enclosed words and escaped single quotes

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM