2

How can I extract strings in a pattern from within another regex pattern Exemple:

REGEXREPLACE("Ex.: <a;b;c;> e <d;e;>.","<(.*?)>","($1)...")

to get

Ex: (a)(b)(c) e (d)(e).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

Suggestion:

You can try a nested REGEXREPLACE functions then use SUBSTITUTE at the end like this sample below:

=SUBSTITUTE(REGEXREPLACE
           (REGEXREPLACE
           (REGEXREPLACE
           ("<a;b;c;> e <d;e;>.", "<(.*?)>", "($1)"),
            "[^a-z()> .a-z]", ")*("),
            "[^a-z()> .a-z]",""),
           "()","")

Sample Demonstration

Sample string is on cell A2

enter image description here

How it works:

  1. Used your original regex:

E.g. placed on cell A2

=REGEXREPLACE("<a;b;c;> e <d;e;>.", "<(.*?)>", "($1)")

Result: (a;b;c;) e (d;e;).

  1. Replace ; with )*(:

=REGEXREPLACE(A2, "[^a-z()> .a-z]", ")*(")

Result: (a)*(b)*(c)*() e (d)*(e)*().

  1. Remove asterisk (*) sign:

=REGEXREPLACE(B2,"[^a-z()> .a-z]","")

Result: (a)(b)(c)() e (d)(e)().

  1. Replace empty () using [SUBSTITUTE][2] function:

=SUBSTITUTE(C2,"()","")

Result: (a)(b)(c) e (d)(e).

Reference:

Multiple regex matches in Google Sheets formula

SputnikDrunk2
  • 3,398
  • 1
  • 5
  • 17