Home > Net >  Regex to match value starting or not starting with a string
Regex to match value starting or not starting with a string

Time:09-30

Inspired by this question, I'm using the following regex to match the total value Total (EUR)?:\s*(.*?)\s*(?:Total?:\s*(.*))?$.

Total (EUR)                     833.00

There may be cases where (EUR) part is missing, so I'd need something to catch the Total value with or without the currency specified between parenthesis.

Total                         1 833.00    

Demo

CodePudding user response:

Here you are the regex string:

Total (?:\s*\(EUR\))*\s*([\d\.\'\s] ?)\s*$

That matches all types of number, including spaces, dots and apostrophes!

Andrea

CodePudding user response:

There are several issues here. First, your regex can look like

Total(?:\s*\(EUR\))?\s*(\d{1,3}(?: \d{3})*\.\d{2})

See the regex demo.

Note:

  • Escape literal parentheses
  • Match whitespace with [ \t]* / * or \s*
  • Put the whitespace in the optional part inside the optional group.

Details:

  • Total - a literal string
  • (?:\s*\(EUR\))? - an optional non-capturing group:
    • \s* - zero or more whitespaces
    • \(EUR\) - (EUR) string
  • \s* - zero or more whitespaces
  • (\d{1,3}(?: \d{3})*\.\d{2}) - Group 1: one, two or three digits and then zero or more occurrences of space and three digits, then . and two digits. Note that this pattern might need further adjustment depending on the number formats you need to support*.

CodePudding user response:

If there can be other currencies or non digits in between, you can match any char other than a digit in between using \D* until the first occurrence of the digits.

Total\s\D*(\d{1,3}(?:[ ,]\d{3})*(?:\.\d{2})?)\b

Regex demo

  • Related