Search code examples
pythonregexmatching

Regex: Extract amounts in Euro


I need a Python regex which extracts amounts in €. For example:

  • 99 €
  • 99.99 €
  • 99,99 Euro
  • 100

I got inspiration from here. Based on this I have build the following regex:

^((\d[\d.\s,]*)(?:\s(?:euro|€|Euro))|\d)$

If you like to see more examples, please see the demo. The regex isn't finished yet. The problem that I face is that it doesn't match the full string.

Maybe you can help me with that? I am not an expert in regex!


Solution

  • You can use

    ^(?:€ *)?(\d(?:[. ,]*\d)*)(?: (?:[eE]uro|€))? *$
    

    Note: if you test against standalone strings, you can replace all literal spaces with \s (any one whitespace) or \s+ (one or more whitespaces).

    Details:

    • ^ - start of string
    • (?:€ *)? - an optional sequence of and zero or more spaces
    • (\d(?:[. ,]*\d)*)
    • (?: (?:[eE]uro|€))? - an optional sequence of a space (add * after it to make it match zero or more spaces), euro, Euro or
    • * - zero or more spaces
    • $ - end of string.

    See the regex demo.