Regex for getting information from square brackets

人走茶凉 提交于 2019-12-01 23:00:21
preg_replace('/\[.*?:(.*?)\]/', '$1', $str);

CodePad.

If you wanted to capture the text before the colon, simply wrap it in parenthesis and it will become capturing group 1.

Update

I'm looking to use preg_match instead of preg_replace

You'd want to use preg_match_all() to get all matches.

preg_match_all('/\[(.*?):(.*?)\]/', $str, $matches);

Ideone.

Alternatively, you could give them named capturing groups.

preg_match_all('/\[(?P<identifier>.*?):(?<body>.*?)\]/', $str, $matches);

Ideone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!