Help

Re: REGEX_REPLACE using expressions in Replacement

Solved
Jump to Solution
2536 0
cancel
Showing results for 
Search instead for 
Did you mean: 
jasonstewart
4 - Data Explorer
4 - Data Explorer

My end goal is to calculate the Flesch–Kincaid readability of a given text field ("Copy (Required)"). In order to do that, I need to determine how many syllables are in that text.

Syllabification is somewhat of a subjective thing, but the approach that I've chosen is to use a series of nested REGEX_REPLACE functions inside a formula field based on this approach from Guilherme D. Garcia. The resulting formula looks like this:

 

REGEX_REPLACE(
  REGEX_REPLACE(
    REGEX_REPLACE(
      REGEX_REPLACE(
        REGEX_REPLACE(
          {Copy (Required)},
          "([aeiou])",
          "\\1-"
        ),
        "-$",
        ""
      ),
      "-([bcdfghjklmnpqrstvxz]$)",
      "\\1"
    ),
    "-(n|r|st)(t|n|d|f)",
    "\\1-\\2"
  ),
  "([aeiou])-s([tpnml])",
  "\\1s-\\2"
)

 

 I know that Airtable’s REGEX functions are implemented using the RE2 regular expression library but I can't seem to get the syntax correct for the "replacement" string. For reference, here's the syntax for REGEX_REPLACE:

 

REGEX_REPLACE(string, regular_expression, replacement)

 

For example, in my innermost "replacement" I am specifying: 

 

\\1-

 

In plain english, this means we will replace whatever is matched with the same match + a hyphen, hence "\\1-". Number 1 here simply refers back to our matching group; since there’s only one group, we use "\\1".

MY PROBLEM: Specifying an EXPRESSION in the "replacement" text doesn't seem to work within Airtable. When I put make "\\1-" the replacement text parameter in REGEX_REPLACE it literally replaces it within that text instead of interpreting it as an EXPRESSION and replacing the matched text with the matched text + a hyphen.

MY QUESTION: Is Airtables REGEX_REPLACE function capable of accepting and processing EXPRESSIONS as the "replacement" parameter? If so, what's wrong with my syntax that I'm getting a literal replacement instead of an expression?

Input text:

Credit

Expected Output (using formula above):

Cre-di-t

Actual Output:

Cr\1-d\1-t

 

1 Solution

Accepted Solutions
TheTimeSavingCo
18 - Pluto
18 - Pluto

Hm, could you try using `$1-` instead of `\\1-`?  (I wish I could tell you why that worked instead but I have no idea either)

The following formula outputs `Cre-di-t` like you want:
`REGEX_REPLACE("Credit", "([aeiou])", "$1-")`

See Solution in Thread

1 Reply 1
TheTimeSavingCo
18 - Pluto
18 - Pluto

Hm, could you try using `$1-` instead of `\\1-`?  (I wish I could tell you why that worked instead but I have no idea either)

The following formula outputs `Cre-di-t` like you want:
`REGEX_REPLACE("Credit", "([aeiou])", "$1-")`