在php正则表达式中的引号之间替换字符串的一部分 [英] Replace part of string between quotes in php regex

查看:270
本文介绍了在php正则表达式中的引号之间替换字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我掌握了非常基本的regex技能,只使用了regex几次就可以了。可能之前有人问过这个问题,对此我深表歉意,但我找不到任何答案。尽管发现类似,并试图对其进行调整,但无济于事。
好​​吧,问题是-如何仅在某些字符(在这种情况下为双引号)之间替换空格?

Now I've got very basic regex skills, only used regex a couple of times for basic stuff. This has probably been asked before, which I apologize for, but I couldn't find any answer for this. Found similar, though and tried to adapt it but to no avail. OK, to the question - How do I replace a space only between certain characters (doublequotes in this case)?

说我有以下字符串: / p>

Say i have the following string:


任务播客 modcast ABC DEF

"mission podcast" modcast A B C "D E F"

我要替换任务播客之间的空格,以及 D E 之间的空格& F ,同时保持其他状态不变。

I want to replace the spaces between mission and podcast as well as the ones between D, E & F whilst leaving the other ones untouched.

P.S。如果空格是一个字符串怎么办?

P.S. What if space was a string? An example for that is welcome as well.

我对此进行了一些编辑,希望现在更加清楚。
编辑2:我需要在php中的字符串上执行此操作,然后在shell中执行它。
编辑3:很抱歉,我将整个问题更改了3次,只是我自己很困惑。干杯!

Edited this a bit I hope now it's more clear. Edit 2: I need to do this on a string in php and execute it in the shell. Edit 3: I'm sorry i changed the whole question 3 times it's just i'm getting quite confused myself. Cheers!

推荐答案

说明



我会首先解决此问题将字符串分成带引号或不带引号的字符串组。

Description

I would attack this problem by first splitting the string into groups of either quoted or not quoted strings.

然后遍历比赛,如果填充了捕获组1,则对该字符串加引号,因此只需对替换捕获组0进行简单替换即可。如果捕获组1为

Then iterating through the matches and if Capture Group 1 is populated, then that string is quoted so just do a simple replace on replace Capture Group 0. If Capture group 1 is not populated then skip to the next match.

在每次迭代中,您只想简单地构建一个新字符串。

On each iteration, you'd want to simply build up a new string.

由于分割字符串是困难的部分,因此我将使用此正则表达式:

Since splitting the string is the difficult part, I'd use this regex:

( [^] * )| [^^] *

示例文本

"mission podcast" modcast A B C "D E F"

代码

PHP Code Example: 
<?php
$sourcestring="your source string";
preg_match_all('/("[^"]*")|[^"]*/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

捕获组

$matches Array:
(
    [0] => Array
        (
            [0] => "mission podcast"
            [1] =>  modcast A B C 
            [2] => "D E F"
            [3] => 
        )

    [1] => Array
        (
            [0] => "mission podcast"
            [1] => 
            [2] => "D E F"
            [3] => 
        )

)



PHP示例



此php脚本将仅替换带引号的字符串中的空格。

PHP Example

This php script will replace only the spaces inside quoted strings.

工作示例: http://ideone.com/jBytL3

代码

<?php

$text ='"mission podcast" modcast A B C "D E F"';

preg_match_all('/("[^"]*")|[^"]*/',$text,$matches);

foreach($matches[0] as $entry){
    echo preg_replace('/\s(?=.*?")/ims','~~new~~',$entry);
    }

输出

"mission~~new~~podcast" modcast A B C "D~~new~~E~~new~~F"

这篇关于在php正则表达式中的引号之间替换字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆