【问题标题】:how to replace dots inside quote in sentence with regex如何用正则表达式替换句子中引号内的点
【发布时间】:2012-10-19 19:15:34
【问题描述】:

让我们说有这样的事情

Lorem ipsum dolor sit amet,consectetur adipiscing elit。 "Vestibulum interdum dolor nec sapien blandit a suscipit arcu fermentum. Nullam lacinia ipsum vitae enim consequat iaculis quis in augue. Phasellus fermentum congue blandit. Donec laoreet, ipsum et vestibulum vulputate, risus augue commodo nisi, vel hendrerit sem justo sed mauris." Phasellus ut nunc neque,id varius nunc。在 enim lectus、blandit et dictum at、molestie in nunc。 Vivamus eu ligula sed augue pretium tincidunt sat amet ac nisl。 "Morbi eu elit diam, sed tristique nunc."

变成这样

Lorem ipsum dolor sit amet,consectetur adipiscing elit。 @ 987654323@ Phasellus ut nunc neque,id varius nunc。在 enim lectus、blandit et dictum at、molestie in nunc。 Vivamus eu ligula sed augue pretium tincidunt sat amet ac nisl。 "Morbi eu elit diam, sed tristique nunc[dot]"

我不知何故找到了一个正则表达式来选择带有"(.)+?" 的所有“{sentence}”或像这样使用它们

regex('"(.)+?"','[sentence]')

但是我们可以做一些类似替换组内的点的事情吗?所以我可以得到上面例子的输出?

【问题讨论】:

  • . 是“任何字符”的字符。如果你想要一个文字点,你需要像\. 一样转义它
  • 不,这只是我进步的一个例子。我在想我们可以使用两次正则表达式,所以我们可以为每个组搜索\.,然后将它们转换为[点]。但也许我们可以在一个 preg 替换函数中做到这一点
  • 您使用什么技术?我认为正则表达式无法单独满足您的需求。
  • php ruby​​ nodejs 我认为它们都是相同的 sp00m,它们有两到三个 args,例如 output = preg_replace('/regex/', what you want to regex, '') 将是 what you want 但我目前正在使用节点。
  • @user1780413 是的,但想法是替换嵌套点,直到字符串不再包含嵌套点。

标签: regex


【解决方案1】:

我不确定正则表达式是否能够自行满足您的需求。

你应该实现一个算法来替换嵌套的点,直到字符串不再包含嵌套的点。

以 PHP 为例:

$string = 'He asked "Please." while she answered "No. Or maybe yes."';
var_dump($string);
while(preg_match('/"[^"]*\.[^"]*"/', $string)) {
    $string = preg_replace('/("[^"]*)\.([^"]*")/', '$1[dot]$2', $string);
}
var_dump($string);

哪个打印:

string 'He asked "Please." while she answered "No. Or maybe yes."' (length=57)
string 'He asked "Please[dot]" while she answered "No[dot] Or maybe yes[dot]"' (length=69)

【讨论】:

    【解决方案2】:

    这就是我会做的。

    echo
    preg_replace_callback('~(?<!\\\)"(.+?)((?<!\\\)")~',
    /*
    Pattern:
    --------
    (?<!\\\)"       a double quote not preceded by a backward (escaping) slash
    (.+?)           anything (with min 1 char.) between condition above and below
    ((?<!\\\)")     a double quote not preceded by a backward (escaping) slash
    */
    // for anything that matches the above pattern
    // the following function is called
    create_function('$m',
    'return preg_replace("~\.~","[dot]",$m[0]);'),
    // which replaces each dot with [dot] and returns the match
    $str);
    

    编辑:在 cmets 中添加了解释。

    【讨论】:

    • 您是否要求提供模式和/或代码解释?让我知道是否是这种情况。我将编辑我的答案。
    • @inhan 当然,如果代码不够简单,您应该解释一下。否则我们应该如何学习?
    • @sp00m 我不确定 OP 是否特别问这个问题或一般如何使用这个 PHP 函数。
    • 不,我正在翻译成 node.js 代码,但我认为人们也需要了解这一点。
    • 您应该在您的问题中使用 Javascript 标记,因为一些重要的规范并不适用于所有正则表达式风格,例如后向和递归模式。出于这个原因,我不得不发布一个单独的答案。
    【解决方案3】:

    试试这个: (\"[^\.]*)\.([^\"]*)\1[dot]\2

    在我的编辑器中运行良好,但有时使用 $ 而不是 \ 代替(例如在 php 中)

    【讨论】:

      【解决方案4】:

      使用 Javascript,我只需进行基本替换:

      str = str.replace(/".+?"/g,function(m) {
          return m.replace(/\./g,'[dot]');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 2020-10-10
        • 1970-01-01
        相关资源
        最近更新 更多