Php 正则表达式以匹配表示西班牙语日期的字符串
我需要使用正则表达式来验证表示完整日期字符串的字符串(用西班牙语编写)。。。我不需要验证实际字符串是否为有效日期(闰年等) 字符串如下所示:Php 正则表达式以匹配表示西班牙语日期的字符串,php,regex,Php,Regex,我需要使用正则表达式来验证表示完整日期字符串的字符串(用西班牙语编写)。。。我不需要验证实际字符串是否为有效日期(闰年等) 字符串如下所示: 23 de septiembre del 2003 23 de septiembre de 1965 如果年份大于2000,则在年份之前使用“del”一词,如果不是,则使用“de”一词 我做了研究,发现了如何获取前两位数: $pattern = ([0-9]+); 。。然后我就不知道怎么把它放在一起 救命啊 说明: \b .
23 de septiembre del 2003
23 de septiembre de 1965
如果年份大于2000,则在年份之前使用“del”一词,如果不是,则使用“de”一词
我做了研究,发现了如何获取前两位数:
$pattern = ([0-9]+);
。。然后我就不知道怎么把它放在一起
救命啊
说明:
\b ... requires a word boundary, since the following character is a digit
(and thus a word character) this will only match if the date is
preceded by a character that is not a letter, not a digit and
not an underscore
\d{1,2} ... one or two digits
de ... literally "de"
[a-z]+ ... any letter from a-z, at least once but an arbitrary number of times
(de 1\d{3} ... literally "de" followed by "1" and 3 more digits
| ... or
del 2\d{3}) ... literally "del" followed by "2" and 3 more digits
i ... make the whole thing case-insensitive (you can omit this if needed)
还要注意,正则表达式中的所有空格都与任何其他字符一样处理
或者,您可以指定有效月份的列表,而不是[a-z]+
,如
/\b\d{1,2} de (...|septiembre|...) (de 1\d{3}|del 2\d{3})/i
(用更多的月份名称替换…并用
|
来分隔它们)~(\d{1,2})de([a-z]+)(del?(\d{4}))~'234 de enero del 2003'匹配项。。。即使第一部分是3位数字,表达式也会验证?为什么?也许php不把它看作一个数字,而是一个字符串?@Marco哦,我明白了。它匹配,因为它只匹配“34”,而忽略2。您可以在前面添加一个单词边界(我会将其编辑到答案中),Patteren应该以^开头,以$结尾。像这样的“/^\d{1,2}de[a-z]+(de 1\d{3}del 2\d{3})$/i”@Marco是的,后一种方法会起作用。但是,您可以这样简化[1-9]|[12][0-9]| 3[01]。。。不过这没什么大不了的
/\b\d{1,2} de (...|septiembre|...) (de 1\d{3}|del 2\d{3})/i