dongwopu8210 2012-09-25 21:08
浏览 318
已采纳

正则表达式匹配表示西班牙语日期的字符串

I need to use a regular expression to validate a string (written in spanish) that represents a full date string... I don't need to validate if the actual string is a valid date (leap years, etc...)

The string looks like this:

23 de septiembre del 2003

23 de septiembre de 1965

If the year is greater then 2000, the the word 'del' is use prior to the year, if not the word 'de' is use...

i did my research and found how to get the first 2 digits:

$pattern = ([0-9]+);

.. then i got lost on how to put it all together...

Help !

  • 写回答

1条回答 默认 最新

  • douji3426 2012-09-25 21:13
    关注
    /\b\d{1,2} de [a-z]+ (de 1\d{3}|del 2\d{3})/i
    

    Explanation:

    \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)
    

    Also note, that all spaces in the regex are treated just like any other character.

    Alternatively, instead of [a-z]+ you could specify a list of valid months like

    /\b\d{1,2} de (...|septiembre|...) (de 1\d{3}|del 2\d{3})/i
    

    (replace ... with more month names an | to separate them)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?