开发者

Regex between < >

开发者 https://www.devze.com 2023-03-22 21:36 出处:网络
I have a stri开发者_如何学Cng that looks like: Pretext<thecontentineed> I\' trying to write a regex that will pull \"thecontentineed\" from that string using preg_match

I have a stri开发者_如何学Cng that looks like:

Pretext<thecontentineed>

I' trying to write a regex that will pull "thecontentineed" from that string using preg_match

I've tried:

$string = "Pretext<thecontentineed>";    
preg_match("/<.*?>/" , $string, $output);

But that returns an empty array.


$string = "Pretext<thecontentineed>";    
preg_match("/<(.*?)>/" , $string, $output);

You forgot the ()


if (preg_match('/<(.*?)>/', $string, $output)) {
    echo $output[1];
}


$string = "Pretext<thecontentineed>";
preg_match("/\<([^>]+)\>/" , $string, $output);
print_r($output);


You haven't specified any capturing groups with ():

preg_match('/<(.*)?>/', $string, $matches);

The () instruct the regex pattern to 'capture' whatever matches within the brackets, and store them into the $matches array.


if (preg_match('/Pretext<(.*?)>/', $string, $output)) {
    echo $output[1];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消