正则表达式中如何同时使用负向回顾断言和正向回顾断言?

huangapple go评论77阅读模式
英文:

regex to use negative look far behind along with positive look behind?

问题

I need it to select only value which has :this in line but not include which are under comment section (/* */)

我需要选择只包含 :this 的值,但不包括在注释部分(/* */)的值。

I have tried this one which works and give 3 results but it is incorrect as it select the last one which is in comment

我尝试过这个方法,它可以工作并返回3个结果,但不正确,因为它选择了在注释中的最后一个值。

Trial 1:

look far behind that is there * in the same line but it does not work and gives 0 result

尝试1:

查找在同一行中是否有 *,但它不起作用,返回0个结果。

英文:

I need it to select only value which has :this in line but not include which are under comment section (/* */)

I have tried this one which works and give 3 results but it is incorrect as it select the last one which is in comment

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = `
    :this { 
      display: grid; 
    }

    p { color: red}

    :this { this is also okay to capture  }
    /* but do not capture this :this { } , so how to look far behind here */
`;

const pattren = &#39;(?&lt;=:)\\bthis\\b\\s*(?={)&#39;;
const rx = new RegExp(pattren, &#39;gmd&#39;);
const matches = str.matchAll(rx);
console.log([...matches]);

<!-- end snippet -->

Trial 1:

look far behind that is there * in the same line but it does not work and gives 0 result

const pattern = &#39;(?&lt;!/*)(?&lt;=:)\\bthis\\b\\s*(?={)&#39;; 

答案1

得分: 1

你可以使用 (?&lt;!/\*.*) 来查找 /* 后面跟着零个或多个字符。

const str = `
       :this { 
          display: grid; 
        }

        p { color: red}

         :this { this is also okay to capture  }
        /* but do not capture this :this { } , so how to look far behind here */
    `;

const re = /(?&lt;!\/\*.*)(?&lt;=:)\bthis\b\s*(?={)/gmd;
const matches = str.matchAll(re);
console.log([...matches]);

请注意,这是你提供的代码的一部分,仅翻译了代码内容。

英文:

You can use (?&lt;!/\*.*) to look for /* followed by zero or more characters.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = `
       :this { 
          display: grid; 
        }

        p { color: red}

         :this { this is also okay to capture  }
        /* but do not capture this :this { } , so how to look far behind here */
    `;

const re = /(?&lt;!\/\*.*)(?&lt;=:)\bthis\b\s*(?={)/gmd;
const matches = str.matchAll(re);
console.log([...matches]);

<!-- end snippet -->

答案2

得分: 1

我会匹配注释块,使其不影响正则匹配。使用捕获组,您可以识别要保留的内容,并丢弃捕获组中没有内容的匹配项。

在这个代码片段中,输出包含匹配项的起始和结束索引:

const str = `
    :this { 
      display: grid; 
    }

    p { color: red}

    :this { this is also okay to capture  }
    /* but do not capture this :this { } , so how to look far behind here */
`;

const rx = /\/\*.*?\*\/|:(this\b\s*)\{/gsd;
const matches = Array.from(str.matchAll(rx), m => m.indices?.[1]).filter(Boolean);
console.log(matches);

请注意,我没有翻译代码部分,只提供了代码的解释。

英文:

I would match the comment block, so it is out of the way. With capture groups you can then identify what you want to keep, and throw away the matches that don't have anything in the capture group.

In this snippet the output has the start-end indices of the matches:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = `
    :this { 
      display: grid; 
    }

    p { color: red}

    :this { this is also okay to capture  }
    /* but do not capture this :this { } , so how to look far behind here */
`;

const rx = /\/\*.*?\*\/|:(this\b\s*)\{/gsd;
const matches = Array.from(str.matchAll(rx), m =&gt; m.indices?.[1]).filter(Boolean);
console.log(matches);

<!-- end snippet -->

答案3

得分: 0

I doubt there is a way achieving what the OP is wishing for with a positive lookbehind but a negative lookbehind like ...

/(?<!\/\*.*?):this\s*\{[^}]+\}/g

... which gets explained at its playground page does the job.

英文:

I doubt there is a way achieving what the OP is wishing for with a positive lookbehind but a negative lookbehind like ...

/(?&lt;!\/\*.*?):this\s*\{[^}]+\}/g

... which gets explained at its playground page does the job.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// see ... [https://regex101.com/r/0p8Uw2/1]
const regXExtractUncommentedThisRule = /(?&lt;!\/\*.*?):this\s*\{[^}]+\}/g;

const sampleData = `
  :this { 
    display: grid; 
  }

  p { color: red}

  :this { this is also okay to capture }
  /* but do not capture this :this { } , so how to look far behind here */
  :this { 
    display: grid; 
  }

  p { color: red}

  :this { this is also okay to capture }
  /* but do not capture this :this { } , so how to look far behind here */`;

console.log(
  sampleData.match(regXExtractUncommentedThisRule)
);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月4日 02:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定