Welcome to WebmasterWorld Guest from 54.198.93.179

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Is such an expression possible?

I want to replace something using wildcards?

     
8:39 pm on Nov 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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!

8:56 pm on Nov 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I figured it out!

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

3:52 am on Nov 17, 2008 (gmt 0)

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



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.
2:17 pm on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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. ;-)
10:09 pm on Nov 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month