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条)

报告相同问题?