SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex for splitting a string?

    I have the following string:

    Code:
    [0001262168300000||Test - Mondrean|45|2|90.00][0001270168300000||Test1 - Mondrean|30|1|30.00]
    I wanted to split the string down in to individual strings based on the [ ] delimiters.

    Therefore in this instance I would have 2 strings.

    I've written the following regex, however I am getting the [ as part of the string.

    Regex looks like this:

    Code:
    /\[*+\]/
    Does anyone have any ideas how I can tidy this up, I'm very rusty on regular expressions!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:

    Sorry, I'm blind.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Each string is surrounded by the [ ] characters.

    [0001262168300000||Test - Mondrean|45|2|90.00]
    [0001270168300000||Test1 - Mondrean|30|1|30.00]

    I therefore want to return only:

    0001262168300000||Test - Mondrean|45|2|90.00
    0001270168300000||Test1 - Mondrean|30|1|30.00

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str "[0001262168300000||Test - Mondrean|45|2|90.00][0001270168300000||Test1 - Mondrean|30|1|30.00]";
    $arr explode("]["$str);
    $str1 ltrim($arr[0], '[');
    $str2 rtrim($arr[1], ']');

    echo 
    $str1 '<br/>';
    echo 
    $str2 '<br/>'

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be an easier way using regular expressions like so:

    Code:
    //Split the deposit_string down
    $items = preg_split('/\[*+\]/', $_POST['deposit_string']);
    
    //Split the items down
    foreach($items as $item) { 
        $deposit_string[] = explode("|", $item);   
    }
    However the regex needs to be less greedy.

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...or a more general solution:
    PHP Code:
    $str "[0001262168300000||Test - Mondrean|45|2|90.00][0001270168300000||Test1 - Mondrean|30|1|30.00]";

    $arr explode("]"$str);

    unset(
    $arr[count($arr)-1]);
    array_walk($arrcreate_function('$a''return ltrim($a);') );

    echo 
    "<pre>";
    print_r($arr);
    echo 
    "</pre>"

  7. #7
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match_all('/(?<=\[).*?(?=\])/'$string$matches);
    print_r($matches); 
    Your strings are in $matches somewhere.
    Edit:

    $matches[0] I think.

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by furious5
    There must be an easier way using regular expressions like so:

    Code:
    //Split the deposit_string down
    $items = preg_split('/\[*+\]/', $_POST['deposit_string']);
    
    //Split the items down
    foreach($items as $item) { 
        $deposit_string[] = explode("|", $item);   
    }
    However the regex needs to be less greedy.
    You need preg_match, not preg_split because you're trying to capture something, not to remove:

    PHP Code:
    $str "[aaa] [bbb] [ccc]";
    preg_match_all("~\[(.*?)\]~s"$str$items);
    $items $items[1];
    print_r($items); 

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str "[0001262168300000||Test - Mondrean|45|2|90.00][0001270168300000||Test1 - Mondrean|30|1|30.00]";

    $pieces preg_match_all("/\[(.*)\]/U"$str$matches);

    echo 
    "<pre>";
    print_r($matches[1]);
    echo 
    "</pre>"
    U is for ungreedy matches.

    String functions are faster, but if you call enough of them, then their efficiency goes down. You might try profiling the string method and the preg method to see which is faster.

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac
    PHP Code:
    preg_match_all('/(?<=\[)(.*?)(?=\])/'$string$matches);
    print_r($matches); 
    Your strings are in $matches somewhere.
    should be .+? not .*?
    although there is no need of lookarounds here IMO

    $pieces = preg_match_all("/\[(.*)\]/U", $str, $matches);
    /U is not recommended because it reduces readability

  11. #11
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    should be .+? not .*?
    although there is no need of lookarounds here IMO
    Yes, you are probably right. I don't think the +/* matters too much as it seems there will always be a few pipes in there somewhere, but I suppose it's it better.
    As for the lookarounds, they are just force of habit when I don't need something. Personal preference I suppose.

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect guys thank you.

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On my system, the preg method I posted is much faster than the general string method I posted. There are too many function calls with the string method.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •