I have very less experience in regexp. I want to get path
structural/designer/00-slider
in one group and name in the second group
00-slider
using regex.
Below is the statement,
{path:structural/designer/00-slider, name:00-slider}, {path:structural/00-1_1, name:00-1_1}, {path:elements/tab, name:tab}
I used this regex,
(.+?)path
I am getting following output,
Match 1
Full match 0-5 `{path`
Group 1. 0-1 `{`
Match 2
Full match 5-58 `:structural/designer/00-slider, name:00-slider}, path`
Group 1. 5-54 `:structural/designer/00-slider, name:00-slider}, `
Match 3
Full match 58-97 `:structural/00-1_1, name:00-1_1}, {path`
Group 1. 58-93 `:structural/00-1_1, name:00-1_1}, {`
How can I achieve this using regex?
You may use
'~{path\s*:\s*(?<path>[^{}]*?), name:(?<name>[^{}]*)}~'
with preg_match_all
. See the regex demo .
Details
{path
- a literal {path
substring \\s*:\\s*
- a colon enclosed with optional 0+ whitespace chars (?<path>[^{}]*?)
- Group "path": any 0+ chars other than {
and }
, as few as possible , name:
- a literal substring (?<name>[^{}]*)
- Group "name": any 0+ chars other than {
and }
, as many as possible }
- a }
char. PHP demo :
$re = '/{path\s*:\s*(?<path>[^{}]*?), name:(?<name>[^{}]*)}/m';
$str = '{path:structural/designer/00-slider, name:00-slider}, {path:structural/00-1_1, name:00-1_1}, {path:elements/tab, name:tab}';
if (preg_match_all($re, $str, $matches)) {
print_r($matches['path']); echo "\n";
print_r($matches['name']);
}
Paths :
Array
(
[0] => structural/designer/00-slider
[1] => structural/00-1_1
[2] => elements/tab
)
Names :
Array
(
[0] => 00-slider
[1] => 00-1_1
[2] => tab
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.