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

    Join Date
    Oct 2009
    Posts
    75
    Rep Power
    5

    Matching the end of each line


    for example i have a JavaScript string like
    PHP Code:
    function  dothat  (xxxsdasdasd)  {
      var 
    ad 15
      
    var xcv 16
      
    var xcD 17;

    I want to check each line that doesn't end with [{|}|;|(|)|,] and add a ";" to its end.

    Thanks.
  2. #2
  3. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi Moktar,

    Try this.

    Input:

    function dothat (xxx, sdasd, asd) {
    var ad = 15
    var xcv = 16
    var xcD = 17;
    }


    Code:
    Code:
    <?php 
    $string = 'function  dothat  (xxx, sdasd, asd)  {
      var ad = 15
      var xcv = 16
      var xcD = 17;
    }';
    $pattern = '~(^[^\r]+?(?<![|{}();,]))(\r)~m';
    $replace = '\1;\2';
    $string = preg_replace($pattern,$replace,$string);
    echo '<pre>'.htmlentities($string).'</pre><br />';
    ?>
    Output:
    function dothat (xxx, sdasd, asd) {
    var ad = 15;
    var xcv = 16;
    var xcD = 17;
    }


    Is this what you want?

    A couple details:
    1. Check the list of excluded characters in the pattern as I was not sure this was exactly what you were asking.
    2. You may want to trim trailing spaces on each line, because we are testing for lines not ending in } (for instance)... And a line ending in "} " would qualify.

    Let me know if this works for you.
    Last edited by ragax; January 16th, 2012 at 11:54 PM. Reason: inserted the code in code tags
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    Here is a slightly modified ragax's solution that skips trailing whitespace:

    Code:
    <?php 
    $string = 'function  dothat  (xxx, sdasd, asd)  {
      var ad = 15 
      var xcv = 16
      var xcD = 17; 
    }';
    $pattern = '~([^{};(),\s][ \t]*)(\r\n|\r|\n|$)~';
    $replace = '\1;\2';
    $string = preg_replace($pattern,$replace,$string);
    echo '<pre>'.htmlentities($string).'</pre><br />';
    ?>
    Note that $ in PHP matches between CR and LF, so I had to use this clumsy construct. If your language/editor has a better support for CR+LF, then use a single $.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    75
    Rep Power
    5
    Thanks a lot folks, that is very helpful. Please suggest me a source for start learning regex more better..
  8. #5
  9. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Originally Posted by Eng_A_Moktar
    Thanks a lot folks, that is very helpful. Please suggest me a source for start learning regex more better..
    Hi Moktar,

    This question comes up a lot, so I've made a detailed reply in this post.

    Wishing you a beautiful day!


    Edit: you asked about learning. For practice (searching and replacing in files), I also recommend abareplace, the tool of the other person who helped on the thread.
    Last edited by ragax; January 17th, 2012 at 04:55 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    75
    Rep Power
    5
    thanks again Ragax, You are so kind person. I hope you the best.
  12. #7
  13. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Originally Posted by Eng_A_Moktar
    thanks again Ragax, You are so kind person. I hope you the best.
    You too, brother. Please feel free to ask anytime.

IMN logo majestic logo threadwatch logo seochat tools logo