douchen1988 2015-03-11 14:05
浏览 48
已采纳

preg_replace with Regex - 在URL中查找数字序列

I'm a regex-noobie, so sorry for this "simple" question:

I've got an URL like following:

http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx

what I'm going to archieve is getting the number-sequence (aka Job-ID) right before the ".aspx" with preg_replace.

I've already figured out that the regex for finding it could be

(?!.*-).*(?=\.)

Now preg_replace needs the opposite of that regular expression. How can I archieve that? Also worth mentioning:

The URL can have multiple numbers in it. I only need the sequence right before ".aspx". Also, there could be some php attributes behind the ".aspx" like "&mobile=true"

Thank you for your answers!

  • 写回答

3条回答 默认 最新

  • dsfdsf46465 2015-03-11 14:10
    关注

    You can use:

    $re = '/[^-.]+(?=\.aspx)/i'; 
    preg_match($re, $input, $matches);
    //=> 146370543
    

    This will match text not a hyphen and not a dot and that is followed by .aspx using a lookahead (?=\.aspx).

    RegEx Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部