Solved

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

Posted on 2014-02-19
2
427 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
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 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 34

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now