22042

regexp add space after period but not when period represents a decimal or letter abbreviation?

Using php regexp in a simple way, is it possible to modify a string to add a space after periods that follow words but not after a period that is preceded and followed by a number such as 1.00? I also need it to ignore single letter abbreviations such as N.Y.

String.Looks like this.With an amount of 1.00 and references N.Y.

Needs to be changed to...

String. Looks like this. With an amount of 1.00 and references N.Y.

This should allow for multiple instances within the string of course...

Answer1:

$text = preg_replace('/(\.)([[:alpha:]]{2,})/', '$1 $2', $text);

Answer2:

You can use this regex:

/((?<=[A-Za-z0-9])\.(?=[A-Za-z]{2})|(?<=[A-Za-z]{2})\.(?=[A-Za-z0-9]))/

And the replacement string is '. '.

Note that the regex above expects at least one alphabetical character on one side of the full stop ., and expects alphanumeric character on the other side.

Test string:

"String.Looks like this.With an amount of 1.00 and references N.Y.Mr.NoOne.30% replaced.no 50.Don't know."

Output:

"String. Looks like this. With an amount of 1.00 and references N.Y. Mr. NoOne. 30% replaced. no 50. Don't know."

Recommend

  • RAILS 3 model validation inclusion in does not work
  • F# instance syntax
  • f:ajax render does not update components anymore
  • How to reference lex or parse parameters in flex rules?
  • Django: Annotate the count of the number of duplicate values for each object
  • Angular `orderBy` filter issue with predicate array
  • What are the valid types for a WebGL vertex shader attribute?
  • How to combine multiple sed commands into one
  • JavaScript querySelectorAll
  • How can I handle Castor unmarshaling of SOAP messages when the namespace is defined inside the opera
  • NumberFormat problem
  • how to remove comments from a bash script
  • converting an object to float in pandas along with replacing a $ sign
  • C++ std::set comparator
  • Prolog Query - Trying to understand how this result happens
  • Does the failbit effect the call ignore on cin?
  • Add spaces between words in spaceless string
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • Messed up characters in webpages (especially social media)
  • LNK1104: cannot open file 'kernel32.lib'
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Can my PDF ping my server when it is opened?
  • Time out Error in send mail
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • Find group of records that match multiple values
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Is there a javascript serializer for JSON.Net?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Excel - Autoshape get it's name from cell (value)
  • angularjs unit test when to use $rootScope.$new()
  • Where to put my custom functions in Wordpress?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Redux, normalised entities and lodash merge
  • Do create extension work in single-user mode in postgres?
  • Return words with double consecutive letters
  • Buffer size for converting unsigned long to string
  • Binding checkboxes to object values in AngularJs