简体   繁体   English

正则表达式提取 2 个正斜杠后面的字符,但忽略两个正斜杠(和后面的文本)是否在双引号之间

[英]Regex to extract characters following 2 forward slashes but ignore if both forward slashes (and following text) are between double quotes

I am working with some string manipulation where I have a multiline text and have to extract text that follows //, but if // (and text) are between double quotes then match should not happen.我正在处理一些字符串操作,其中我有一个多行文本,并且必须提取 // 后面的文本,但是如果 //(和文本)在双引号之间,则不应发生匹配。 A sample of the text I am working with is below:我正在处理的文本示例如下:

This a line // tester 7897这是一行 // 测试员 7897
//Ola //奥拉
asdfasdf自卫队
//554654 //554654
Open("asd//Not this")打开(“asd//不是这个”)

From the above text I'm expecting the intended Regex to return me the following matches从上面的文字我期待预期的正则表达式返回给我以下匹配

// tester 7897 // 测试员 7897
//Ola //奥拉
//554654 //554654

I have tried quite a few options but the following regex (with Regex Options Multiline) is the closest I have got to is following one:我尝试了很多选项,但以下正则表达式(使用 Regex Options Multiline)是我最接近的一个:

(//).+ (//).+

This gives me all matches following // and that includes //Not this from the last line (which I don't want).这为我提供了 // 之后的所有匹配项,其中包括//Not this来自最后一行(我不想要的)。

I don't have a lot of experience using Regex.我没有很多使用 Regex 的经验。 Any help will be greatly appreciated.任何帮助将不胜感激。

I do not think this is possible.我不认为这是可能的。 You want to match everything after // only if it is not surrounded by "" .只有当它没有被""包围时,你才想要匹配//之后的所有内容。 That would require negative lookaround, and you cannot use negative lookbehind in this case where the number of characters between the first " and // is not fixed.这将需要负环视,并且在第一个"//之间的字符数不固定的情况下,您不能使用负环视。

Try this.试试这个。

^(?:[^\"\n]|\\\")*(?:(?<!\\)\"(?:[^\"\n]|\\\")*(?:(?<!\\)\")(?:[^\"\n]|\\\")*)*((?:\/\/).*)

Here is the link for regex101 .这是regex101的链接。

Here is the reference .这是参考

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

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