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

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

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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。