Thread: Regex help

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    60
    Rep Power
    7

    Regex help


    am new to regex so please help.
    i want to split my string based on "=","#" and whitespace
    PHP Code:
    $a preg_split("/#|=| /""name     =abcd   # 132542 "
    but this returns each whitespace in different element of the array. what i want to get in the array is
    $a[0] = name
    $a[1] = (empty space)
    $a[2] = abcd and so on
    any help is appreciated
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    PHP Code:
    preg_split("/[#=\s]+/""name     =abcd   # 132542 "
    We have a regular expression forum for future questions.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    60
    Rep Power
    7
    Originally Posted by ManiacDan
    PHP Code:
    $result preg_split("/[#=\s]+/""name         =abcd            #132542 "
    We have a regular expression forum for future questions.

    -Dan
    this still does not solve my problem. I want to include whitspaces in the result. so the result should be:
    $result[0] = name
    $result[1] = (whitespace between name and =)
    $result[2] = abcd
    $result[3] = (whitespace between abcd and #)
    $result[4] = 132542

    i want to obtain this result after my regex split.

    please move it to that forum because i don't want to create another thread. thanks
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    thread moved.

    So you want to break on whitespace, but keep any other whitespace you find? That's significantly more difficult, but still possible:
    PHP Code:
    preg_split("/[#=\s](?![#=\s])/""name         =abcd            #132542 "
    This leaves an empty final match at the end (because of the trailing space) but you can pass anything through trim to fix it, unless you want any trailing space in its own match. If you want the trailing space to be associated with the last number there...I can't think of a quick way to do that. A quick post-check will allow you to put the last two array elements together if the last one is nothing but whitespace.

    Incidentally, if this is a standard format, you'd be better off writing a regexp that assumes you'll have "string<space>=string<space>#string<space>"

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    60
    Rep Power
    7
    Originally Posted by ManiacDan
    thread moved.

    So you want to break on whitespace, but keep any other whitespace you find? That's significantly more difficult, but still possible:
    PHP Code:
    preg_split("/[#=\s](?![#=\s])/""name         =abcd            #132542  "
    This leaves an empty final match at the end (because of the trailing space) but you can pass anything through trim to fix it, unless you want any trailing space in its own match. If you want the trailing space to be associated with the last number there...I can't think of a quick way to do that. A quick post-check will allow you to put the last two array elements together if the last one is nothing but whitespace.

    Incidentally, if this is a standard format, you'd be better off writing a regexp that assumes you'll have "string<space>=string<space>#string<space>"

    -Dan
    thanks for the help dan. but i am still not there. i want to break on end of line. so this is format of the line that i have
    "string<space>=string<space>#string<cr>"
    with your regex am not able to get the whitespaces in between. this is what i got
    PHP Code:
    $k preg_split("/[#=\s](?![#=\s])/""name     =abcd   # 132542 ") ;
    print_r($k); 
    and i got this output
    Array ( [0] => name [1] => abcd # [2] => 132542 [3] => )

    how can i get the whitespaces in between. and reminder i want whitespaces together not separately.again thanks for the help
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    You do have the whitespaces in between, you're just viewing it on a webpage so the whitespaces are truncated. In an HTML document, any number of whitespaces (tabs, newlines, spaces, etc) show up as a single space.

    Use:
    PHP Code:
    echo "<pre>" print_r($whatever1) . "</pre>"
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    60
    Rep Power
    7
    Originally Posted by ManiacDan
    You do have the whitespaces in between, you're just viewing it on a webpage so the whitespaces are truncated. In an HTML document, any number of whitespaces (tabs, newlines, spaces, etc) show up as a single space.

    Use:
    PHP Code:
    echo "<pre>" print_r($whatever1) . "</pre>"
    -Dan
    Dan really appreciate your help. Thanks

IMN logo majestic logo threadwatch logo seochat tools logo