开发者

How to extract a string with a regexp?

开发者 https://www.devze.com 2023-03-25 20:57 出处:网络
Can I extract the string aaa without using subpatterns (but still using regexp)? $str = \'abc#aaa#abc\';

Can I extract the string aaa without using subpatterns (but still using regexp)?

$str = 'abc#aaa#abc';
preg_match('/#(.*)#/', $str, $matches);
print_r($matches);

Update

  • lookahead and lookbehind is a second solution, any other ?

I'm just trying to figure out some regexp alternat开发者_如何转开发ives !


/(?<=#).*?(?=#)/

But why wouldn't you want to use a capturing group?


Alternative to look-ahead / look-behind assertions:

echo preg_replace('/^.*?#|#.*?$/s', '', 'abc#aaa#abc');
0

精彩评论

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

关注公众号