Split on every character inside square brackets


Split on every character inside square brackets



I have a problem with a regex, I have the following string


[date|Y-m-d]



which is a Token extracted from a text and I need to extract 'date' and 'Y-m-d', but I haven't been able to achieve my goal with the following regex, can someone help me


(?:[)([^|]+)(?:])



The tokens can be like:


[date|Y-m-d] should extract 'date' and 'Y-m-d'
[date|Y-m-d|today] should extract 'date', 'Y-m-d' and 'today'





Use: $tok = preg_split('/[|]/', $str, -1, PREG_SPLIT_NO_EMPTY) and then use $tok[0] and $tok[1]
– anubhava
Jun 29 at 9:21


$tok = preg_split('/[|]/', $str, -1, PREG_SPLIT_NO_EMPTY)


$tok[0]


$tok[1]





@claudio See this demo. It is for scenarios where only 1 match per string is expected.
– Wiktor Stribiżew
Jun 29 at 9:53





3 Answers
3



Use a regex pattern


(?<=^[||)[^|[]]*(?=||]$)



Test this regex solution here and a related PHP code here.





Thanks, that's what I was trying to achieve
– claudio
Jun 29 at 13:27



You can also achieve this by removing bracket with ltrim() & rtrim(). Then you can use explode with ":".


$str = explode(':', ltrim( rtrim($str, ']'), '['));
print_r( $str );



Output:


Array
(
[0] => date
[1] => Y-m-d
)



I assume you may have strings containing those substrings inside brackets and you want to extract them.



You may use


$str = '[date|Y-m-d] [date|Y-m-d|today]';
preg_match_all('/[([^]+)]/', $str, $matches);
$res = ;
foreach ($matches[1] as $m) {
array_push($res,explode("|", $m));
}
print_r($res);



See the PHP demo



Also, see the identical solution for scenarios where only 1 match per string is expected (PHP demo):


$str = 'A single match example [date|Y-m-d|today] here.';
if (preg_match('/[([^]+)]/', $str, $matches)) {
print_r(explode("|", $matches[1]));
}



Details


'/[([^]+)]/'


[


]


|



If you just have a string equal to [...|...|...|etc.] you may either use Shivrudra's approach, or a very simple preg_match_all:


[...|...|...|etc.]


preg_match_all


$str = '[date|Y-m-d]';
preg_match_all('/[^][|]+/', $str, $matches);
print_r($matches[0]);



See another PHP demo.



The [^][|]+ pattern matches 1 or more chars other than [, ] and |. See the regex demo.


[^][|]+


[


]


|





Thanks, it helped. But my real problem is capturing an undetermined number of strings
– claudio
Jun 29 at 9:19





@claudio please update your question to clarify your actual problem then.
– Gordon
Jun 29 at 9:20





@claudio See this demo. It is for scenarios where only 1 match per string is expected.
– Wiktor Stribiżew
Jun 29 at 9:54






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV