The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
PHP - Matching the end of each line
Discuss Matching the end of each line in the Regex Programming forum on Dev Shed. Matching the end of each line Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 16th, 2012, 08:20 PM
|
|
Contributing User
|
|
Join Date: Oct 2009
Posts: 75
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 (xxx, sdasd, asd) {
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.
|

January 16th, 2012, 11:54 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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
|

January 17th, 2012, 01:03 AM
|
|
|
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 $.
|

January 17th, 2012, 03:54 PM
|
|
Contributing User
|
|
Join Date: Oct 2009
Posts: 75
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..
|

January 17th, 2012, 04:52 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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.
|

January 17th, 2012, 05:15 PM
|
|
Contributing User
|
|
Join Date: Oct 2009
Posts: 75
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.
|

January 17th, 2012, 05:38 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|