dongmaxi6763 2013-09-02 21:51
浏览 60
已采纳

正则表达式替换特定标记内所有出现的单个字符

I would like to know if a single set of regex search/replace patterns could be used to replace all occurrences of a specific character inside a string contained within 2 tokens.

For example, is it possible to replace all periods with spaces for the text between TOKEN1 & TOKEN2 as in the example below?

So that:

TOKEN1:Run.Spot.run:TOKEN2

is changed to:

TOKEN1:Run Spot run:TOKEN2

NOTE: The regular expression would need to be capable of replacing any number of periods within any text, and not just the specific pattern above.

I ask this question more for my personal knowledge, as it is something I have wanted to do quite a few times in the past with various regex implementations. In this particular case, however, the regex would be in php.

I am not interested in php workarounds as I know how to do that. I am trying to expand my knowledge of regex.

Thanks

  • 写回答

3条回答 默认 最新

  • douxitao8170 2013-09-02 22:22
    关注

    A way to do this:

    $pattern = '~(?:TOKEN1:|\G(?<!^))(?:[^:.]+|:(?!TOKEN2))*\K\.~';
    $replacement = ' ';
    $subject = 'TOKEN1:Run.Spot.run:TOKEN2';
    $result = preg_replace($pattern, $replacement, $subject);
    

    pattern details:

    ~                  # pattern delimiter
    (?:                # open a non capturing group
        TOKEN1:        # TOKEN1:
      |                # OR
        \G(?<!^)       # a contiguous match but not at the start of the string
    )                  # close the non capturing group
    (?:                # open a non capturing group
        [^:.]+         # all that is not the first character of :TOKEN2 or the searched character
      |                # OR
        :(?!TOKEN2)    # The first character of :TOKEN2 not followed by the other characters
    )*                 # repeat the non capturing group zero or more times
    \K                 # reset the match
    \.                 # the searched character
    ~                  # delimiter
    

    The idea is to use \G to force each match to be TOKEN1: or a match contiguous with the precedent match.

    Notice: the default behavior is like an html tag (it is always open until it is closed). If :TOKEN2 is not found all the \. characters will be replaced after TOKEN1:.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀