Thread: String Editing

    #1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595

    String Editing


    I have the following 3 statements for editing a string:
    Code:
    $parts[0]=~s/^\s+|\s+$//g;
    $parts[0]=~s/\"//g;
    $parts[0]=~s/\\//g;
    While it does the job, not being a regex expert, I'm wondering if/how they can be combined into a single statement. TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,954
    Rep Power
    1225
    The 2nd and 3rd could be combined.
    Code:
    $parts[0]=~s/[\"]//g;
    Your first regex is how I normally strip leading/trailing spaces, however it's actually slightly more efficient to split that up into 2 regex's.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Thanks. Does the [] remove the need to escape back slash and quote or should it be:
    Code:
    $parts[0]=~s/[\\\"]//g;
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,954
    Rep Power
    1225
    Sorry, my bad.

    You need to escape the backslash, but not the double quote.

    Code:
    s/[\\"]//g;

IMN logo majestic logo threadwatch logo seochat tools logo