82306

Detecting and replacing merged words MyWord to My

Question:

I have a function which is detecting and unmerging two words i.e.

HelloWord

after function will become

Hello World

the problem is it will do same to iPhone -> i Phone which is not good, is there a way to replace only if first word is longer than 1

here is my current function (regex):

function unseparateWords($string) { $CapRegX = '/(\B[A-Z])(?=[a-z])|(?<=[a-z])([A-Z])/sm'; $RepStr = ' $1$2'; return preg_replace($CapRegX,$RepStr,$string); }

thanks for help.

cheers, /Marcin

Answer1:

You could search for the position between a lowercase and an uppercase character

(?<=\B[a-z])(?=[A-Z])

and replace this "zero-length string" with a space.

(?<=\B[a-z]) # assert that we are right after a lowercase ASCII character # unless that character is the start of the current word (?=[A-Z]) # assert that there is an uppercase ASCII character right ahead

In PHP:

function unseparateWords($string) { return preg_replace('/(?<=\B[a-z])(?=[A-Z])/', ' ', $string); }

Recommend

  • Detecting and replacing merged words MyWord to My Word - PHP, regex
  • Automatically change the position of objects when using different devices
  • iOS, How to check whether the user likes a facebook page or not?
  • Apple Reachability Notifications for Network OR Wi-Fi
  • Android NDK vs iOS - performance issue [closed]
  • Can't run Appium tests on iOS 10 on real device
  • google-maps-sdk-ios - arm 7/iphone 5 issues for Google Maps 1.4.3 for iOS
  • Retrieving a contacts notes
  • Installing apk from within application in android
  • How to merge two Request in Laravel
  • Localization issue Xcode
  • Unique SMS sender id?
  • Get variable height for a UIButton for iPhones 5/6/6+
  • Can my app be notified when another application starts/stops playing audio?
  • Why does Sencha 2 only work in Webkit browsers?
  • retrieving data from url in iphone
  • What causes the runtime difference in this trivial fortran code?
  • Access user's phone number on iOS 7
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • jQuery: How to AJAXify WordPress Search?
  • SAXReader not re-ecape characters
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • What and where is mdimport
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Declaring variable dynamically in VB.net
  • Checking free space on FTP server
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Regex thinks I'm nesting, but I'm not
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Incrementing object id automatically JS constructor (static method and variable)
  • Join two tables and save into third-sql
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?