Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
lfzyx
V2EX  ›  Linux

用正则匹配了字符串中的字符,如何增加到到字符串的结尾?

  •  
  •   lfzyx ·
    lfzyx · Jan 22, 2019 · 3011 views
    This topic created in 2851 days ago, the information mentioned may be changed or developed.
    比方说 Hello “ Alice ” , today is Thu, 用正则匹配到了 Alice,然后改成 Hello “ Alice ” , today is Thu, Alice
    7 replies  •  2019-02-04 14:06:58 +08:00
    weizhen199
    weizhen199
    Jan 22, 2019
    1
    $
    weizhen199
    weizhen199
    Jan 22, 2019
    2
    啊,当我没说,正则没有取值再赋值这种功能,你可能要用一些其他的工具
    masker
    masker
    Jan 22, 2019 via Android
    3
    替换啊。分组替换。
    zjp
    zjp
    Jan 22, 2019❤️1
    4
    假设 `正则匹配到了 Alice` 就是匹配双引号里的内容,可以用 ^(.*"(.*)".*)$ 替换为 \1\2
    总觉得不优雅...
    quinoa42
    quinoa42
    Jan 22, 2019
    5
    用 python 的话很简单,bash 也不难

    ```bash
    caught=$(echo "$1" | grep --only-matching "Alice")
    output="$1,$caught"
    echo $output
    ```
    lfzyx
    lfzyx
    OP
    Jan 22, 2019
    6
    用 sed 的括号 加上 \1\2 解决了
    dcmmc
    dcmmc
    Feb 4, 2019
    7
    `capture group` 了解一下, 就像 `lfzyx` 说的.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   930 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 21:23 · PVG 05:23 · LAX 13:23 · JFK 16:23
    ♥ Do have faith in what you're doing.