Regexp to remove specific number of occurrences of character only

前端 未结 2 789
名媛妹妹
名媛妹妹 2020-12-12 05:19

In Python re, I have long strings of text with > character chunks of different lengths. One string can have 3 consecutive > char

相关标签:
2条回答
  • 2020-12-12 05:58

    no need to split on spaces first if dont needs to

    try (?<![^ ])[^ >]*>>[^ >]*(?![^ ])

    finds segments on space boundry's with only >> in it and no more

    0 讨论(0)
  • 2020-12-12 06:11

    You need to make sure there is no character of your choice both on the left and right using a pair of lookaround, a lookahead and a lookbehind. The general scheme is

    (?<!X)X{n}(?!X)
    

    where (?<!X) means no X immediately on the left is allowed, X{n} means n occurrences of X, and (?!X) means no X immediately on the right is allowed.

    In this case, use

    r'(?<!>)>{2}(?!>)'
    

    See the regex demo.

    0 讨论(0)
提交回复
热议问题