4

I am very new to regex and just cannot figure out how to write a pattern to match what I need. Any help would be awesome!

I want to use PHP & regex to capture each set of characters in a string that follow a specific unique character (delimiter), plus any set of characters that precedes the first instance of that delimiter. I then want to "match" the desired output into a PHP array.

  • Example delimiter: >
  • Example string:

    $str = 'word1 > word-2 > word.3 > word*4';
    
  • My desired match:

    array([0] => 'word1', [1] => 'word-2', [2] => 'word.3', [3] => 'word*4',);
    

I've looked through the following responses, and while they are close, they don't quite help me achieve what I need:

This is the PHP function I'm currently working with, but it currently only finds the characters between the delimiter:

function parse_conditions($str, $delimiter='>') {
if (preg_match_all('/' . $delimiter . '(.*?)' . $delimiter . '/s', $str, $matches)) {
    return $matches[1];
}

NOTE: the number of items in a given string may vary, so I can't use a pattern that expects a specific number of delimiters (ex. /^(.*?)>(.*?)>(.*?)>$/)

Community
  • 1
  • 1

3 Answers3

4

You can simply use array_map along with explode as

$str = 'word1 > word-2 > word.3 > word*4';
$result = array_map('trim',  explode('>', $str));
print_r($result);

Output:

Array
(
    [0] => word1
    [1] => word-2
    [2] => word.3
    [3] => word*4
)

You can Check it here

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • This is perfect, thank you! So many great answers, and multiple ways to accomplish the same thing. As a mentioned, I'm just learning, so didn't even know about explode. Thanks again. – Dylan Moffett May 23 '15 at 16:36
  • I'm glad to hear that you liked the way. Thank you for that appreciation. You can accept one of these answers for future users. Thanks again – Narendrasingh Sisodia May 23 '15 at 19:46
1

As mentioned, you could just use explode for this:

$str = 'word1 > word-2 > word.3 > word*4';
print_r(explode(" > " , $str));

However for completeness sake, let's also use RegEx.

In this case, we can tell the Regular Expression to group all characters together that aren't whitespace and aren't the delimiter >:

preg_match_all('/([^>\s]+)/', $str, $matches);
echo print_r($matches[0]);

# [0] => Array
#    (
#        [0] => word1
#        [1] => word-2
#        [2] => word.3
#        [3] => word*4
#    )
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • This is a great, complete answer. Combined with the answer from Uchiha (to trim whitespace), I can use either explode or regex to accomplish what I need. Thank you. I am curious if one method is preferred over the other? To your knowledge Is explode more efficient than preg_match_all? – Dylan Moffett May 23 '15 at 16:39
  • I would use explode in this case since this is what it is designed for, and is readily apparent to other programmers what you are doing. RegEx on the other hand is not so immediately obvious. – Martin Konecny May 23 '15 at 16:42
1

I would use preg_split, http://php.net/preg_split, for this.

<?php
$matches = preg_split('~\s*>\s*~', 'word1 > word-2 > word.3 > word*4');
print_r($matches);

Output:

Array
(
    [0] => word1
    [1] => word-2
    [2] => word.3
    [3] => word*4
)

The \s* means any number of whitespace characters.

This is similar to using an explode.

<?php
$matches = explode('>', 'word1 > word-2 > word.3 > word*4');
print_r($matches);

but as you see with the explode you have the whitespaces:

Array
(
    [0] => word1 
    [1] =>  word-2 
    [2] =>  word.3 
    [3] =>  word*4
)
chris85
  • 23,846
  • 7
  • 34
  • 51
  • Thanks for the answer - As I'm trying to learn, I really appreciate the explanation of what's happening in the split. There have been three great answers that each accomplish what I need. I'm curious if you have any info on why preg_split is better than the explode + trim answer from uchiha or the preg_match_all answer from martin-konecny. Or perhaps they are just simply different ways to do it, with no clear advantage either way. Thanks again. – Dylan Moffett May 23 '15 at 16:45
  • There are many different ways to do things in programming. I prefer to have the least number of function calls as possible which is why I went with the `preg_split`. @Uchiha answer seems like the easiest to read to me. – chris85 May 23 '15 at 18:38
  • 1
    Using `explode()` one can use `$matches = array_map('trim', $matches);` to strip whitespaces. Just test, what's faster for you. – rabudde Jan 30 '20 at 09:09