Вопрос

I need to create a regex which captures all different types of currency such as:

£1
$100,000,000
€25.00
25p       (pence)
25c       (cents)
25m       (million)
25bn      (billion)
25 million
25 billion
£0.25

Currently, I've got the following:

^(([^0]{1})([0-9])*|(0{1}))(\.\d{2})?$

This works for:

£100
200 -- don't want this to be included
$200
€400
350 -- don't want this to be included

Any help please?

Это было полезно?

Решение

You could try a pattern like this:

^([£€$]([0-9]([0-9,])*)(\.\d{2})?|([0-9]([0-9,]))(\.\d{2})?([pcm]|bn| [mb]illion))$

This will match either:

  • A £, , or $ followed by a number which may contain commas, followed by an optional . followed by two more digits.
  • A number which may contain commas, followed by an optional . followed by two more digits, followed by p, c, m, bn, or a space, followed by million or billion.

Here's a demonstration

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top