简体   繁体   English

正则表达式pcre替换不匹配组的匹配组?

[英]regex pcre replace matched group by not matched group?

someone can give me an example for example for this string 有人可以给我一个例子来说明这个字符串

wordride plain fire

replace the spaces by another letter 用另一个字母替换空格

for example replace 例如替换

the first match space for the third letter before the space 空间前第三个字母的第一个匹配空间

for example i want to replace character after word using this 例如,我想用这个替换字符

   (?:\G(?!^)|word)\K.

replace character by word usign a "word" as group 用字替换字符将“字”作为组

      (?:\G(?!^)|(word))\K.

using $1$2 使用$ 1 $ 2

but not work 但不行

how could solve this problem 怎么能解决这个问题

in this case 在这种情况下

     wordride plain fire

for example when the algorithm search "r" replace this by the group "word" 例如,当算法搜索“r”用组“word”替换它时

obtain 获得

     wordwordide plain fire 

i want to find all letter after word character by character ( ride .......) and replace by group word using subtitution $1 $2 etc 我希望逐字逐字地找到所有字母(骑.......),并用字组$ 1 $ 2等替换组字

please help me 请帮我

To replace letter after word by captured group, you can use: 要更换后的信word被抓获组,您可以使用:

$s = 'wordride plain fire';

$r = preg_replace('/(?>(word)|\G(?<!^))\K\S/', '$1', $s, 1);

//=> wordwordide plain fire 

RegEx Demo RegEx演示

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

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