Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP/REGEX: Remove last unfinished sentence were period ends sentence

Posted on 2014-02-19
2
Medium Priority
?
454 Views
Last Modified: 2014-02-19
Using PHP and REGEX, how can I remove the last incomplete sentence?   For example,
Hello, world.  Joe D. is a
Should become:
Hello, world.

This code works if there are no initials followed by periods:
$mystring = preg_replace("/[\w\s',]*$/",'',$mystring);

Open in new window

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28369053.html

Any period that follows one single letter does not end a sentence.

Thanks!
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 39871629
This will work for the example provided:
$mystring = preg_replace("/([^!.?]|[A-Z]\.)*$/",'',$mystring);

Open in new window


However, when the initial is an upper case letter i then there is a very small chance (depending on your data) that it's not actually an initial and it's part of the sentence instead, as "I" is a word.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39871662
$mystring = preg_replace("/[\w\s\',]*(( \w\.)*[\w\s\',]*)*$/",'',$mystring);

Open in new window


This works for all the following cases:
Hello, world.  Joe D. is a
Hello, world.  Joe is a
Hello, World. Joe
Hello, world. Joe B. joe d.
Hello, world. b. jor d.

HTH,
Dan
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question