使用正则表达式忽略带有数字的字符串中的日期 [英] Ignore date in a string with numbers using regular expression
问题描述
我有一个小问题.
我使用[0-9\,.]*
查找字符串中的小数.
然后([^\s]+)
查找第一个数字后面的文本.
i use [0-9\,.]*
to finde a decimal in a string.
And ([^\s]+)
to find the text behind the first number.
字符串通常看起来像这样. 1个数字,而不是日期:
The string looks normally like this. 1 number a text and than a date:
1.023,45stück
1.023,45 stück
24.05.10
但是有时我只有日期,然后我变成240510(十进制). 有时我只有十进制数.
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.
谢谢.
推荐答案
分而治之
-
先检查日期,然后从字符串中删除匹配项
Check for the date first and remove the match from the string
([0-9]{1,2}\.){2}[0-9]{1,2}
使用原始正则表达式查找号码
Find the number using your original regex
[0-9\,.]*
如果需要,可以找到数量单位(假设u Umlaut仅将其小写)
If you need it find the unit of quantity (assuming that you will only have it as lower case with u Umlaut)
([a-zü]+)
请参见 http://regexe.de/(德语)和
See http://regexe.de/ (German) and http://www.regexr.com/ (English) for some useful information and tools for dealing with regex.
这篇关于使用正则表达式忽略带有数字的字符串中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!