Search code examples
regexnotepad++regex-group

Match characters class after the pattern


With a text:

//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/...
//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/...
//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/...
//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/...

I would like to match all the [ and ] after first ... . (To remove them. Replace them with non/empty char in Notepad++)

I know that:

  • [\[\]] - matches ALL square brackets
  • (?<=\.\.\.).* - matches only second part of the string (after ...)

I cannot join this two operation together.

((?<=\.\.\.).*)[\[\]]+ will create following group: //test/asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq

When I would rather have groups only with square brackets inside.

Is it even possible to run regex query on matched group?

Demo

Goal: Remove all square brackets from all right hand side paths only.

To be:

//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/asdmaopifndqpw/asdkjlha/safd/waoqjerpq/124152/124152-app/1jr-2rj1/...
//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/asdmaopifndqpw/asdkjlha/safd/waoqjerpq/124152/124152-app/1jr-2rj1/...
//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/asdmaopifndqpw/asdkjlha/safd/waoqjerpq/124152/124152-app/1jr-2rj1/...
//asdj/[asdmaopifndqpw]/[asdkjlha]/[safd]/[waoqjerpq]/124152/124152-app/1jr-2rj1/... //test/asdj/asdmaopifndqpw/asdkjlha/safd/waoqjerpq/124152/124152-app/1jr-2rj1/...

Solution

  • You may use this regex for matching:

    (?:\.{3}|(?!^)\G)[^][]*\K[][]
    

    Replace it with empty string.

    Updated RegEx Demo

    RegEx Breakup:

    • (?:: Start non-capture group
      • \.{3}.*?: Match 3 dots followed by 0 or more of any character
      • |: OR
      • (?!^)\G: \G asserts position at the end of the previous match or the start of the string for the first match. (?!^) makes sure we start matching from end of the previous match
    • ): End non-capture group
    • [^][]*: Match 0 or more of any char that are not [ and ]
    • \K: Reset matched info
    • [][]: Match a [ or ]