Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2012, 08:20 PM
Eng_A_Moktar Eng_A_Moktar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 75 Eng_A_Moktar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 46 sec
Reputation Power: 4
PHP - 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.

Reply With Quote
  #2  
Old January 16th, 2012, 11:54 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
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

Reply With Quote
  #3  
Old January 17th, 2012, 01:03 AM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation 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 $.

Reply With Quote
  #4  
Old January 17th, 2012, 03:54 PM
Eng_A_Moktar Eng_A_Moktar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 75 Eng_A_Moktar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 46 sec
Reputation Power: 4
Thanks a lot folks, that is very helpful. Please suggest me a source for start learning regex more better..

Reply With Quote
  #5  
Old January 17th, 2012, 04:52 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
Quote:
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.

Reply With Quote
  #6  
Old January 17th, 2012, 05:15 PM
Eng_A_Moktar Eng_A_Moktar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 75 Eng_A_Moktar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 46 sec
Reputation Power: 4
thanks again Ragax, You are so kind person. I hope you the best.

Reply With Quote
  #7  
Old January 17th, 2012, 05:38 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > PHP - Matching the end of each line

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap