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 September 12th, 2012, 03:27 PM
preludecrazy preludecrazy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 preludecrazy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 52 sec
Reputation Power: 0
Stripping whitespace and commas from end of multilines

Hi.

Please can someone help me with a regex that almost works fine!

I want it to strip off any whitespace, commas, newlines, and CRs off the end of multiline text; then add ", " at the end of each line. I've almost got it right. BTW, the text will be from a HTML textarea form field.

For example, I want...

Sunnyside,
10, High Street ,
Cambridge. .
Cambridgeshire.
CB1 1BC,.,

...To look likes this afterwards:

Sunnyside, 10, High Street, Cambridge, Cambridgeshire, CB1 1BC,

Note: There could be zero or more whitespace surrounding a comma or full-stop adjacent to the newline or CR. Also note I'd want to leave the ", " following "10" as it is, because I'm just trying to clean up the stuff towards the end of each line so I can reformat it myself. Although, seen as I'm joining it up into a single line it wouldn't matter, I guess!?

I'm doing this in PHP, but the pattern should be familiar to most. This is what I got so far:
$message = preg_replace("/[\s,\.]*[\n\r\z]/", ", ", $address);

This produces the following output, using my example:
Sunnyside, 10, High Street, Cambridge, Cambridgeshire, CB1 1BC,.,

As you can see, it appears to work fine except for the last line, which doesn't have a newline. I was hoping the "\z" would solve that.

BTW, I don't actually want the replacement string (", ") at the very end, but I'll do something else to chop that off.

I suppose an alternative to 'replacing' is to instead 'match' into an array, then join back together with a ", " delimiter.

Thanks.

Reply With Quote
  #2  
Old September 16th, 2012, 05:40 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 190 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 50 m 57 sec
Reputation Power: 41
Try this:
Code:
$result = preg_replace('/[\s,.]*[\s\r]*$/m', ',', $subject)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Stripping whitespace and commas from end of multilines

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