Looking for an efficient regex to do this
/url-example-123.shtml
& /url-example-25.shtml
to /url-example-3
Objectives:
remove the
.shtml
replace with
3
if the numbers are between25
and125
Since you didn't give a language I can only give you a regex. You can use the following to match numbers between 25 & 125: (?:(?:1[0-1][0-9])|(?:12[0-5]))|(?:[^\d](?:2[5-9])|(?:[^\d](?:[3-9][0-9])))
. You can then just do a replace with that match and a 3. If there will always be a .shrml at the end then you could add that to the end of the expression.
The question is vague. Not sure what should happen if there is no number, or if the number is outside the given range. Now that you've specified PHP, I've edited my answer:
echo preg_replace(
"/(?<=\/url-example-)(2[5-9]|[4-9][0-9]|1[01][0-9]|12[0-5]).shtml/",
"3",
myUrl);
精彩评论