Michael Ng
Michael Ng

Reputation: 37

Extracting a number in string inside brackets with Regexextract

I am trying to extract number only (float?) from accounting numbers in google sheet with abbrev. units like K,M,B and sometimes in a bracket when negative. Sorry I am so new in regex, how to write a regular express covering different possibilities like (213M),(31.23B)?

\(([0-9.]+\.\[0-9.]+)\)

Upvotes: 1

Views: 540

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

You may use

\((-?\d+(?:\.\d+)?)[KMB]\)

Details

  • \( - a literal ( char
  • (-?\d+(?:\.\d+)?) - Group 1:
    • -? - an optional -
    • \d+ - 1+ digits
    • (?:\.\d+)? - an optional non-capturing group matching one or zero occurrences of a dot followed with 1+ digits
  • [KMB] - a character class matching K, M or B
  • \) - a literal ) char.

See the regex demo.

Upvotes: 1

Related Questions