简体   繁体   English

尝试匹配此正则表达式:PHP

[英]Trying to match this regular expression: PHP

EDIT: I am trying to match the year in the string below 编辑:我试图匹配下面的字符串中的年份

   $expression = "BORN ON:</th><td>weekday, Month Day, Year 
hr:min:sec AM timezone</td>"

AM maybe pm , timezone could be pst etc year is always 20-something 可能是pm AMtimezone可能是pst年份总是20左右

I've tried: 我试过了:

$pattern = "~BORN ON:</th><td>\w+(/20\d\d/)\w+</td>~";

to no avail, help is appreciated! 无济于事,感谢您的帮助!

Thanks 谢谢

you should backslash " / " symbols. 您应该反斜杠“ / ”符号。 and make sure you have leading and trailing slash in your expression eg 并确保您的表达式中有正斜杠和斜杠

$pattern = "~BORN ON:<\/th><td>\w+(\/20\d\d/)\w+<\/td>~";

preg_match("/...../", $where, $results);

secondly, here is updated expression: 其次,这里是更新的表达式:

preg_match("/BORN ON:<\/th><td>\w, \w [0-9]+, [0-9]+ [0-9]+:[0-9]+:[0-9]+ (AM|PM) \w <\/td>/i", $where, $results);

not tested, but should match your needs. 未经测试,但应符合您的需求。 also, enclose parts that you need to appear in results - atm, all date would be found in $results[1] if matched. 同样,将需要显示在结果中的部分括起来-atm,如果匹配,则所有日期都可以在$ results [1]中找到。


upd 更新

preg_match("/BORN ON:<\/th><td>(\w, \w [0-9]+, ([0-9]+) [0-9]+:[0-9]+:[0-9]+ (AM|PM) \w) <\/td>/i", $where, $results);

$results[1] now would contain year $ results [1]现在将包含年份

Try to get this string separately : 尝试单独获取此字符串:

$string_time = "weekday, Month Day, Year hr:min:sec AM timezone" ;
// you use str_replace to get it

then use php builtin function : 然后使用php内置函数:

$the_time = strtotime($string_time);

now you can manipulate it anyway you want, if that's what you are looking for. 现在,如果您正在寻找它,就可以随心所欲地对其进行操作。

You can use date("...",$string_time) to get it in any format you want. 您可以使用date("...",$string_time)以所需的任何格式获取它。 ex: 例如:

echo date("Y-m-d",$string_time);

I matched using this in Ruby. 我在Ruby中使用了它。 Regex is prolly same in PHP. 正则表达式在PHP中完全相同。 Try it out. 试试看。 Let me know if you have any issues. 如果您有任何问题,请告诉我。 I would love to help. 我很乐意提供帮助。 Good luck! 祝好运! :) :)

string = "BORN ON:</th><td>weekday, Month Day, Year hr:min:sec AM timezone</td>"
regex = /<\/th><td>([^,]+)\s*,\s*([^ ]+)\s+([^,]+)\s*,\s*([^ ]+)\s+([^:]+):([^:]+):([^ ]+)\s+(AM|PM)([^<]+)<\/td>/i
if regex.match(string)
    puts "match"
    puts "#{$1} is weekday"
    puts "#{$2} is month"
    puts "#{$3} is day"
    puts "#{$4} is year"
    puts "#{$5} is hour"
    puts "#{$6} is minutes"
    puts "#{$7} is seconds"
    puts "#{$8} is Am or Pm"
    puts "#{$9} is timezone"
else
    puts "no match"
end

我得到这个工作:

"~BORN ON:</th><td>\w+, \w+ [0-9]+, ([0-9]+)~";

How about something like this to extract the year? 这样提取年份的方法怎么样?

$strs = array( 
    "BORN ON:</th><td>weekday, Month 05, 2011 hr:min:sec AM timezone</td>",
    "BORN ON:</th><td>Sunday, Jan 05, 2010 hr:min:sec AM timezone</td>",
    "BORN ON:</th><td>Tuesday, 11 05, 2019 hr:min:sec AM timezone</td>"    
);

foreach( $strs as $subject)
    if( preg_match('%BORN ON:</th><td>\w+,\s*\w+\s*\w+,\s*(\d+)\s*%im', $subject, $regs))
        echo $regs[1] . "\n";

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM