替换任何regex-php


Replace anything regex php

我在php中有这个正则表达式

$array_item_aux = str_replace('/.*PUBMED=/',"",$array_item);

它应该替换这个文本(-|ENSR00001252129|RegulatoryFeature|regulatory_regon_variant|-|-||-|PUBMED=21499247

用这个

21499247

我做错了什么

或者,如果显示的字符串是整个字符串,则可以使用爆炸:

$array_item_aux = explode('PUBMED=', $array_item)[1];

如果您的PHP版本太旧(<5.4),无法使用此语法,则可以使用:

$tmp = explode('PUBMED=', $array_item);
$array_item_aux = $tmp[1];

或者正如@Sam所建议的:

list(, $array_item_aux) = explode('PUBMED=', $array_item);

str_replace不使用正则表达式,使用preg_replace:

$array_item_aux = preg_replace('/.*?PUBMED=/', "", $array_item);

您应该通过添加?使.*变为懒惰而不是贪婪;但您的主要问题是str_replace()不允许使用regex进行搜索,而是使用preg_replace():

$array_item_aux = preg_replace('/.*?PUBMED=/', '', $array_item);