简体   繁体   English

Flutter 中的正则表达式查找双引号括起来的单词和转义的单引号

[英]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 :我在我的 Flutter 应用程序中使用此正则Regex来查找以.tr结尾的single-quotes起来的单词:

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 .现在我需要另一个几乎相同的表达式,但查找由dobule-quotes起来的单词,以.tr结尾并且可能包含转义的单引号

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.我尝试简单地将第一个表达式的单引号更改为双引号,但是 Flutter 给我错误...我需要转义一些字符,但我无法使其工作。 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 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:您可以将此正则表达式用于双引号文本,该文本可以包含任何转义字符,后跟.tr和单词边界:

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

RegEx Demo正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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