正则表达式为欧元金额

时间:2017-11-07 11:23:25

标签: regex

我需要找到一个只选择金额(以欧元为单位)的正则表达式,因此该值需要以euros开头,并且在,之后我们有便士,也可以有空格或点。

7 967  59 €
- 9847, 48 euros à titre de rappel de salaire sur le bonus de l'année 2012,
 - 1929, 78 euros à titre de rappel de salaire sur le bonus de l'année 2013,
  - 129 689, 78 euros à titre de solde d'indemnité conventionnelle de licenciement,
- 1098 euros au titre du paiement du DIF,
é à 20 892, 05 euros, il ressort des pi
le de 27 084, 26 euros
ée à 26 395, 10 euros, hors bo
 de 129 689, 78 euros,
6.000 € au titre des dommages et intérêts pour licenciement sans cause réelle et sérieuse,
 1.510 € au titre de l'indemnité compensatrice de préavis,
 151 € au titre des congés payés y afférents, 739 € au titre de l'indemnité de licenciement,
 656,19 € au titre de l'indemnité due au titre de la non rémunération de la période de mise à pied conservatoire,
 65,61 € au titre des congés payés afférents,
 2.000 € au titre de  59 € au titre de <span class="highlight_underline">l'indemnité légale de licenciement</span>
2014,7 967, 59 € au titre de <span class="highlight_underline">l'indemnité légale de licenciement</span>
rappel de salaires de janvier 2007 au 7 mars 2007 3.708,34 €
SECTION B N° 419 425 426 427 428 429 430 432 433 434 436 441 442 443 444 446 467 571 572

我想出了这个:

(\d.+\d+)(?:\s(?:euros?|€))

但它并不准确。

有人能帮帮我吗?

编辑:

@WiktorStribiżew给了我:

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

虽然很接近但有这个例子:

2014,7 967, 59 €

它也需要2014,

49715 11000158926 101,30 €

需要49715 11000158926。数字仅限于3人。

2007 3.708,34 €

它也不应该取2007

编辑2:

感谢您的回答,但似乎无法在我的python脚本中运行:

import regex
sentences_pd = pd.read_csv('sampled_amounts.csv', names=["text"])
sentences_pd.head()
print([(regex.findall("\b((?:\d+|\d{1,3}(?:[,.\s]\d{3})*)(?:[,.\s]*\d+)?)\s(?:euros?|€)", x)) for x in sentences_pd['text']])

文本列如下所示:

enter image description here

它给了我一个空数组

[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

2 个答案:

答案 0 :(得分:3)

您可以使用

\b((?:\d+|\d{1,3}(?:[,.\s]\d{3})*)(?:[,.\s]*\d+)?)\s(?:euros?|€)

请参阅regex demo

<强>详情

  • \b - 字边界
  • ((?:\d+|\d{1,3}(?:[,.\s]\d{3})*)(?:[,.\s]*\d+)?) - 第1组
    • (?: - 一个轮换组开始
      • \d+ - 1+位数
      • | - 或
      • \d{1,3} - 1至3位数字
      • (?:[,.\s]\d{3})* - 0+序列
        • [,.\s] - 1个空格,,.
        • \d{3} - 3位数
    • ) - 交替小组结束
    • (?:[,.\s]*\d+)? - 可选组
      • [,.\s]* - 0+个空格,,.
      • \d+ - 一位或多位
  • \s - 空白
  • (?:euros?|€) - euroeuros

答案 1 :(得分:0)

如果有帮助,我以西班牙价格(€)创建了此Regex。条件是:

1.- The decimal point with 2 exact decimals
2.- Decimals can not be "00"
3.- The point of a thousand is not admitted. In my case the prices do not exceed 999 €
4.- The front and back spaces are not allowed
5.- One ¨0¨ is not admitted in front of a whole number

Regex: ^((0\,(?!00)\d{2})|([1-9]\d*(\,(?!00)\d{2})?)|0)$

允许的值:

  • 0
  • 1234
  • 0,10
  • 12,34

不允许的值:

  • 0,00
  • 0,1
  • 1234(开头的空格)
  • 1234(末尾有空格)
  • 12,00
  • 01,23
  • 12,345
  • 1.234