SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basic RegEx Qeustion

    Im trying to strip the whitespace from the front and back of an {,

    This RegEx is brain boggling, here's wjat I have:

    HTML Code:
    /[\s]"{"[\s]/

    Here's what i understand kind of

    \s for any space, tab, etc..
    "{" for the literal character
    / and / to start the regex area,
    [] for little boxes of there own (not needed?)

    (ps: no need for a long explanation I dont know If Ill understand it well anywayS)

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    /\s*\{\s*/
    maybe...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is rite, thank you

    I got some spacing before the bracket is that a known reason
    PHP Code:
    <?php

    $string 
    'item {   here;';

    $pattern '/s*\{\s*/';
    $replacement '${1}{$2';

    $newstring preg_replace($pattern$replacement$string);

    ?>

    <textarea><?php echo $string?></textarea>
    <textarea><?php echo $newstring?></textarea>
    Code:
    $pattern = '/.s*\{\s*/';
    Okay the Period seems to work there (unless its two spaces or more)


    I think I need to use a + (The + (plus) matches the previous character 1 or more times, for example, tre+ will find tree and tread but not trough.)

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $pattern '/\s+(?={)|(?<={)\s+/';
    $replacement ''
    EDIT: Disregard this. Unless you want to make your life complicated

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Nothing too complicated required.
    PHP Code:
    $sA 'item     {           here;';
    $sB preg_replace'/\s*\{\s*/''{'$sA );

    var_dump$sA$sB ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey that works, Ill try to understand, well maybe not.. haha
    Thanks :P


    \s+ Space and any amount before it
    (?={) Match This and ignore maybe,
    | string the left and right together
    {?={) Do the same thing above,
    \s+ Space any amount after

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you understand it correctly except the pipe means or. Solution suggested by logic_earth is simpler and works just as well (even better in terms of performance), though.

    I had a subconcious urge to overcomplicate things at the time of writing my regex

  9. #9
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay thanks for your time guys, I have a lot to practice to understand this, thats a great webiste too,
    Ah I didnt think | was or, thought it had to be || XD

  10. #10
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JREAM View Post
    thats a great webiste too,
    Best one around.

    Quote Originally Posted by JREAM View Post
    Ah I didnt think | was or, thought it had to be || XD
    Not in regexes.


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
  •