Search code examples
regexgoogle-sheetsgoogle-sheets-formularepeatre2

Regular expression that matches a pattern as well as any number of comma space separated repetitions


What regular expression matches the below

  • a pattern that's composed of two letters and numbers
  • any comma-space repetitions of said pattern

Assume the below cells, their contents and the times the pattern is repeated

Cell Content Repetitions
A1 CC148 0
A2 CC249, CC193 1
A3 CC345, CC856, CC749 2

For simplicity, I will refer to the regular expression I need as myRegEx

Content Formula Expected Output
CC148 REGEXMATCH(A1, myRegEx) TRUE
CC249, CC193 REGEXMATCH(A2, myRegEx) TRUE
CC345, CC856, CC749 REGEXMATCH(A3, myRegEx) TRUE

The below expressions for each respective cell would return true, but the problem is that I'm after one expression that gets the job done, independently of the number of repetitions.

  • CC\d+
  • CC\d+, CC\d+
  • CC\d+, CC\d+, CC\d+

The below expression didn't bring the desired outcome

  • (CC\d+)|.

Solution

  • try:

    =INDEX(REGEXMATCH(" "&A1:A10&",", REPT(" CC\d+,", 
     LEN(REGEXREPLACE(A1:A10, "[^,]", ))+1)))
    

    enter image description here