23

Is it possible to define a part of the pattern once then perhaps name it so that it can be reused multiple times inside the main pattern without having to write it out again?

To paint a picture, my pattern looks similar to this (pseudo regex pattern)

(PAT),(PAT), ... ,(PAT)

Where PAT is some lengthy pattern.

Requirements

  1. Not have to repeat the pattern because it's length becomes a problem (currently, Notepad++ only allows 2047 characters in the search box when using regex and I'm easily going over this limit)
  2. Each capturing group should be able to match independently of its siblings. For example, say that my pattern is ([a-z]),([a-z]),([a-z]) then a,a,a and a,b,c should match

I've looked into naming the first capturing group then referencing it in the subsequent capturing groups but this method breaks the second requirement (i.e., it fails to match a,b,c). Is there a direct or indirect way of fulfilling both requirements using regex only?

My end goal is to be able to get and access the value of each capturing group so I can manipulate each group later in the "replace" part of the search & replace box.

Mr.Z
  • 542
  • 2
  • 5
  • 18

1 Answers1

39

To reuse a pattern, you could use (?n) where n is the number of the group to repeat. For example, your actual pattern :

(PAT),(PAT), ... ,(PAT)

can be replaced by:

(PAT),(?1), ... ,(?1)

(?1) is the same pattern as (PAT)whatever PAT is.

You may have multiple patterns:

(PAT1),(PAT2),(PAT1),(PAT2),(PAT1),(PAT2),(PAT1),(PAT2)

may be reduced to:

(PAT1),(PAT2),(?1),(?2),(?1),(?2),(?1),(?2)

or:

((PAT1),(PAT2)),(?1),(?1),(?1)

or:

((PAT1),(PAT2)),(?1){3}
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Perfect, thank you! As a side note, I had to enclose the references `(?n)` with one extra level of parentheses so that I can access them later in the replace part. Pattern ended up as `(PAT),((?1)), ... ,((?1))` – Mr.Z Jan 26 '17 at 17:21
  • Is this possible also in javascript? – Adrian Moisa May 10 '18 at 16:40
  • @AdrianMoisa It is not possible in JS, it is only possible with PCRE compatible regex (Python PyPi `regex` module copied it). `\g` is also working in PCRE and Onigmo/Oniguruma (Ruby). – Wiktor Stribiżew May 27 '18 at 14:07
  • Is it possible to reuse group, if this non-capturing group like `(?:)`? – Mykola Vasilaki Mar 02 '20 at 12:41
  • 3
    @NicholasVasilaki: No it is not. You could reuse group only if it is a capture group. – Toto Mar 02 '20 at 12:43
  • 1
    @Serg: That's completly wrong, `\1` is a backreference to group 1 and `(?1)` repeat the **pattern** they are 2 distinct things. – Toto Feb 25 '21 at 17:32
  • In perl these are "Recursive subpatterns", see the `perlre` man page. – mr.spuratic Dec 22 '22 at 15:55
  • @Toto `\1` is actually what I was looking for thanks! Do you have any links to these for further reference? – Akaisteph7 May 16 '23 at 16:37
  • @Akaisteph7: https://www.regular-expressions.info/ – Toto May 16 '23 at 18:12
  • @Toto I see [backreferences](https://www.regular-expressions.info/backref.html) `\1`, not the other one `(?1)`. – Akaisteph7 May 16 '23 at 18:25
  • @Akaisteph7: `\1` is a backreference, it repeats the **value** captured in group 1 ; `(?1)` repeats the **pattern** defined in group 1. – Toto May 16 '23 at 18:32
  • @Toto Yeah thanks, I am asking for what it is called or a reference/link for reading more about it. – Akaisteph7 May 16 '23 at 20:23