Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    regex syntax help

    hi all,

    doing some regex pattern searching, i'm trying to search for the pipe character...
    |

    but the pattern im using does not pick it up, do i need to escape it somehow?

    the pattern im using is :

    '/^|/'

    any thoughts?
    cheers,
    sir p

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Yeah, the pipe means or in a regex...escape character is the \...
    '/^\|/'

    Good luck;

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    thanks Brandoe.. cheers

    something that has cropped up from that, it seems when the pipe is in my text string, anything that follows a pipe is ignored..

    var was:
    $data = "abc|123";
    the 123 is ignored but if i do ..
    $data = "abc123|";
    it works fine...

    any thoughts?
    cheers,
    P

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    What is your php code?

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Use preg_match_all() instead of preg_match()
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    here it is:

    PHP Code:
    $subject "abcd|ef";
    $pattern '/^\|/';
    preg_match($patternsubstr($subject,1), $matchesPREG_OFFSET_CAPTURE);
    print_r($matches); 
    Hi Mar... I used all, just changed the above to _all and it completed the script but did not fill the array, seems it didnt pick up the pipe..

    any pointers?
    many thanks for your time both of you, much apperciated.
    P

  • #7
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    What are you trying to do here? That regex matches any string with a pipe as the first character but doesn't save a match at all (no parens).

    PHP Code:
    $subject "abcd|ef"

    # Both parts
    print_r(explode('|'$subject)); 

    # First part
    preg_match('/\A(.*?)[|]/xms'$subject$match);
    print_r($match);

    # Last part
    preg_match('/[|](.*)\z/xms'$subject$match);
    print_r($match);

    # Position of the separator
    print_r(strpos($subject'|')); 
    Generally, if you have pipe separated data to deal with it's time to refactor your data structure to use something more portable and less error-prone, but YMMV.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Ralph thanks for your reply. Sorry for my delayed responce.

    In answer to your question, all I'm trying to do is extract everything after a pipe if the pipe is there.. is there is no pipe, nothing happens at all....

    I will play with your code now. I hope my answer provides more info
    cheers for that

    Sir P

  • #9
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You dont need to use regular expression to extract everyting after the|char, you can use explode() which also is faster in this case.

    ex

    <?php

    $string = "Exctract everything|after the pipe char";
    list(, $var) = explode('|', $string, 2);

    echo $var;

    ?>

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    fantastic.. explode.. so simple.. brilliant!
    thanks very much

  • #11
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    PHP Code:
    preg_match('/.*?\|(.*?)/',$string,$post_pipe);
    echo 
    $post_pipe[1]; 
    But yea, explode is a cleaner solution - regex isn't always the answer
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready


  •  

    Posting Permissions

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