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!

我是一个正则表达式noobie,很抱歉这个“简单”的问题: p> \ n

我有一个如下网址: p>

  http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-mw-Job-Mainz-Rheinland  -Pfalz-Deutschland-146370543.aspx 
  code>  pre> 
 
 

我要做的就是在“.aspx”之前获得数字序列(又名Job-ID) “使用preg_replace。 p>

我已经发现找到它的正则表达式可能是 p>

 (?!。*  - )  。*(?= \。)
  code>  pre> 
 
 

现在preg_replace需要与正则表达式相反。 我怎么能这样做呢? 另外值得一提的是: p>

URL中可以包含多个数字。 我只需要在“.aspx”之前的序列。 此外,“。aspx”背后可能有一些php属性,如“& mobile = true” p>

感谢您的回答! p> div>

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

You can just use preg_match (you don't need preg_replace, as you don't want to change the original string) and capture the number before the .aspx, which is always at the end, so the simplest way, I could think of is:

<?php
$string = "http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx";
$regex = '/([0-9]+)\.aspx$/';
preg_match($regex, $string, $results);
print $results[1];
?>

A short explanation:

$result contains an array of results; as the whole string, that is searched for is the complete regex, the first element contains this match, so it would be 146370543.aspx in this example. The second element contains the group captured by using the parentheeses around [0-9]+.

You can get the opposite by using this regex:

(\D*)\d+(.*)

Working demo

MATCH 1
1.  [0-100] `http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-`
2.  [109-114]   `.aspx`

Even if you just want the number for that url you can use this regex:

(\d+)