Anand Sainath
Anand Sainath

Reputation: 1807

PHP - String pattern matching

I need to know if there is any way I can do the following using regular expressions (or otherwise) in PHP

aaBaa BBB -> aa Baa BBB

ie, I want to introduce a space before a capital letter only if a Capital letter occurs before and after a small letter.

The best I could come up with was something like this

$string = preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', $string);

but that would only give me something like

aaBaa BBB -> aa Baa B B B

Thanks in advance.

Upvotes: 0

Views: 985

Answers (2)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230326

Here: http://rubular.com/r/3xqbuWuiLD

([a-z]+)([A-Z]+)

Upvotes: 1

piotrekkr
piotrekkr

Reputation: 3181

Try this:

preg_replace('|([a-z])([A-Z])([a-z])|', '$1 $2$3', $txt);

Upvotes: 2

Related Questions