homepage Welcome to WebmasterWorld Guest from 23.22.173.58
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Is such an expression possible?
I want to replace something using wildcards?
maximillianos

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3786530 posted 8:39 pm on Nov 14, 2008 (gmt 0)

Hello!

I'm trying to see if there is a way to reformat the following sentence:

"Hello World.This sentence runs into the previous."

to:

"Hello World. This sentence runs into the previous."

Note that the only change in format is an added "space" after the end of the first sentence period.

Can such a replace be done via perl expressions?

I tried the following to no avail:

$my_sentence =~ s/\w\.\w/\w\. \w/g;

Any ideas if this is possible?

Thanks!

 

maximillianos

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3786530 posted 8:56 pm on Nov 14, 2008 (gmt 0)

I figured it out!

$my_sentence =~ s/(\w)\.(\w)/$1. $2/g;

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 3786530 posted 3:52 am on Nov 17, 2008 (gmt 0)

depending on your requirements, you might want to consider a few more things.
- there are other sentence-ending punctuations than a period.
- there are other contexts for using a period than a sentence-ending punctuation such as within a dollar amount, a url, etc.

maximillianos

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3786530 posted 2:17 pm on Nov 22, 2008 (gmt 0)

Very good points. I actually had to code some fixes in to repair situations like those you mentioned. Particularly around dollar amounts and Urls. But it is definitely helping more than hurting from a formatting perspective. You would not believe how many folks type sentences without putting a space after each period. ;-)

timster

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3786530 posted 10:09 pm on Nov 25, 2008 (gmt 0)

Here's a Thanksgiving gift. You seem to be working hard on this problem.

s/(?<=[A-Za-z])(\.)\s*(?=[A-Z](?:[a-z]¦ ))/. /g;

This one shouldn't mess up prices or URL's (unless the URL's are mixed-case, which I don't see much of these days). It will ensure exactly one space following qualifying periods, so it will handle too many or too few.

Just FYI:

?<= is a lookbehind assertion
?= is a lookahead assertion
[A-Za-z]is a character class

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved