Home > database >  Match characters class after the pattern
Match characters class after the pattern

Time:09-15

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/...

CodePudding user response:

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 ]

CodePudding user response:

Fiddling with Answer given by @anubhava I found that by simply making:

\.{3}.*?\K[\[\]] it's possible to remove RHS square brackets.

RegEx Breakup:

  • \.{3}.*?: Match 3 dots followed by 0 or of any char.
  • \K: Reset matched info
  • [\[\]]]: Match either [ or ] char

Pros

  • No need to insert anything in field Replace with
  • Only one short command to be pasted.

Cons

  • Multiple Replace All button press is needed. (As many as number of brackets to be removed)

I would really like it if it would be possible to do it in on go, it's not perfect and I hope there's a better solution.

  • Related