Ignore date in a string with numbers using regular

2019-07-20 22:15发布

I have a little Problem.

i use [0-9\,.]* to finde a decimal in a string. And ([^\s]+) to find the text behind the first number.

The string looks normally like this. 1 number a text and than a date:

1.023,45 stück

24.05.10

but sometimes I had just the date and then i become 240510 as decimal. And sometimes I had just the decimal.

How should I modify the regex to find the date if existing and remove it? And then look for a decimal an select this if existing.

Thanks in advance.

2条回答
Luminary・发光体
2楼-- · 2019-07-20 22:41

I suggest matching the number in a more restricted way (1-3 digits, then . + 3 digits groups if any, and a decimal separator with digits, optional).

(?s)(?<number>\d{1,3}(?:\.\d{3})*(?:,\d+)?)\s+(.*?)(?:$|\n|(?<date>\d{2}\.?`\d{2}\.?(?:\d{4}|\d{2})))

See demo

The number will be held in ${number}, and the date in ${date}. If the string starts with something very similar to a date (6 or 8 digits with optional periods), it won't be captured. If the date format is known (say, the periods are always present), remove the ?s from \.?s.

(?s) at the beginning will force the period . to match a new line (maybe it is not necessary).

查看更多
\"骚年 ilove
3楼-- · 2019-07-20 22:45

Divide and conquer

  1. Check for the date first and remove the match from the string

    ([0-9]{1,2}\.){2}[0-9]{1,2}

  2. Find the number using your original regex

    [0-9\,.]*

  3. If you need it find the unit of quantity (assuming that you will only have it as lower case with u Umlaut)

    ([a-zü]+)

See http://regexe.de/ (German) and http://www.regexr.com/ (English) for some useful information and tools for dealing with regex.

查看更多
登录 后发表回答