Search code examples
regexgoogle-sheetsgoogle-sheets-formula

Spreadsheet RegexReplace but exclude non-number


How to RegexReplace but only for number.

enter image description here


I tried this formula, but it didn't go as I expected:
=REGEXREPLACE(A2, ":<BR CLASS="""">[0-9]", ": [0-9]")

The link of sheet is here.


Solution

  • You can use \d+ (or [0-9]+) to match one or more digits, and a capturing group (a pair of unescaped parentheses) around the pattern you want to keep, with a backreference to the group value in the replacement:

    =REGEXREPLACE(A2, ":<BR CLASS="""">(\d+)", ": $1")
    

    enter image description here

    Details:

    • :<BR CLASS= - a fixed string
    • """" - a "" pattern (since a " char should be escaped with another " in a string literal in Google Sheets formula)
    • > - a > char
    • (\d+) - Capturing group 1 ($1 refers to the group value): one or more digits.