Help

Upcoming database upgrades. Airtable functionality will be reduced for ~15 minutes at 06:00 UTC on Feb. 4 / 10:00 pm PT on Feb. 3. Learn more here

REGEX_REPLACE using expressions in Replacement

Topic Labels: Regular Expressions
Solved
Jump to Solution
136 1
cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

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-")`