[英]Regex: Matching a part of last part of URL?
I have a lot lines like this: 我有很多这样的话:
<b>http://www.mywebsite.com/video/xvxkxf_article1_something</b>
<b>http://www.mywebsite.com/video/xvxkxi_article2_something</b>
How to match "article1_something" and "article2_something" ? 如何匹配“ article1_something”和“ article2_something”? Thank you ! 谢谢 !
.*?_(.*)</b>$
Try this.Grab the capture.See demo. 试试看,获取捕捉,请看演示。
http://regex101.com/r/yP3iB0/4 http://regex101.com/r/yP3iB0/4
or 要么
^.*?_|</b>
Try this.Replace with empty string
.See demo. 试试这个。用empty string
替换。请参阅演示。
http://regex101.com/r/yP3iB0/5 http://regex101.com/r/yP3iB0/5
Here's a regex free version: 这是一个正则表达式免费版本:
<?php
$str = "<b>http://www.mywebsite.com/video/xvxkxf_article1_something</b>";
$str = substr($str, 0, strlen($str) - 4); // removing </b>
$ar = explode("/", $str); // break url into array with / as delimiter
$ending_path = end($ar); // xvxkxf_article1_something
$ending_path_ar = explode("_", $ending_path); // break xvxkxf_article1_something with _ as delimiter
array_shift($ending_path_ar); // remove xvxkxf
$res = implode("_", $ending_path_ar); // join together pieces with _ as glue.
echo $res; // article1_something
?>
这是您的正则表达式:
/article\d_something/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.