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)

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条