简体   繁体   English

PHP-字符串模式匹配

[英]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 我需要知道是否可以使用PHP中的正则表达式(或其他方式)执行以下操作

aaBaa BBB -> aa Baa BBB 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 BBB aaBaa BBB-> aa Baa BBB

Thanks in advance. 提前致谢。

尝试这个:

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

此处: http//rubular.com/r/3xqbuWuiLD

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM