Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 仅当字符不在括号中时替换字符_Python_Regex - Fatal编程技术网

Python 仅当字符不在括号中时替换字符

Python 仅当字符不在括号中时替换字符,python,regex,Python,Regex,我有一个字符串,如下所示: test_string = "test:(apple:orange,(orange:apple)):test2" 我只想将“:”替换为“/”,前提是它不包含在任何一组括号中 所需输出为“test/(苹果:橙色,(橙色:苹果))/test2” 如何在Python中实现这一点 找到第一个左括号 查找最后一个右括号 将第一个圆括号前的每一个“:”替换为“/” 不要对中间部分做任何事 将最后一个右括号后的“:”替换为“/” 把这3个子串放在一起 代码: 输出: test/(

我有一个字符串,如下所示:

test_string = "test:(apple:orange,(orange:apple)):test2"
我只想将“:”替换为“/”,前提是它不包含在任何一组括号中

所需输出为“test/(苹果:橙色,(橙色:苹果))/test2”

如何在Python中实现这一点

  • 找到第一个左括号
  • 查找最后一个右括号
  • 将第一个圆括号前的每一个“:”替换为“/”
  • 不要对中间部分做任何事
  • 将最后一个右括号后的“:”替换为“/”
  • 把这3个子串放在一起
  • 代码:

    输出:

    test/(apple:orange,(orange:apple))/test2
    

    警告:正如评论所指出的,如果有多个不同的括号,这将不起作用:(

    您可以使用下面的代码获得预期的输出

    def solve(args):
        ans=''
        seen = 0
        for i in args:
            if i == '(':
                seen += 1
            elif i== ')':
                seen -= 1
            if i == ':' and seen <= 0:
                ans += '/'
            else:
                ans += i
        return ans
    
    test_string = "test:(apple:orange,(orange:apple)):test2"
    print(solve(test_string))
    
    def solve(args):
    ans=“”
    SEED=0
    对于args中的i:
    如果i=='(':
    SEED+=1
    如果i==')':
    见-=1
    如果i==':'并与模块一起查看:

    • \((?:[^()]+++)(?0))++\)
      递归匹配一对偏执词
      • 有关说明,请参阅
    • (*跳过)(*F)
      以避免替换前面的模式
      • 有关说明,请参阅
    • |::
      指定为备用匹配

    所需输出?@user5173426请查看编辑的帖子。谢谢!检查字符,在
    上增加计数器(
    ,在
    上减少)
    ,如果字符匹配且计数器为
    0
    ,则替换。执行愉快!这不会缩放任意深度的嵌套括号。适用于此特定示例;不适用于符合OP描述的一般情况(例如:
    (苹果:橙色):测试:(橙色:苹果)
    )太酷了!!!!如果你不介意的话,你可以分享你从哪里学习这类正则表达式的资源吗?@Mayur来自很多地方,其中一些有from
    regex
    标签,所以,一些来自答案中提到的链接。我在github上放了一个,可能会有帮助,谢谢!如果我想检查它是否在Brake内,而不是括号呢ts
    {…}
    ?@JackArnestad将所有括号更改为大括号,以
    开头的对除外(?
    def solve(args):
        ans=''
        seen = 0
        for i in args:
            if i == '(':
                seen += 1
            elif i== ')':
                seen -= 1
            if i == ':' and seen <= 0:
                ans += '/'
            else:
                ans += i
        return ans
    
    test_string = "test:(apple:orange,(orange:apple)):test2"
    print(solve(test_string))
    
    >>> import regex
    >>> test_string = "test:(apple:orange,(orange:apple)):test2"
    >>> regex.sub(r'\((?:[^()]++|(?0))++\)(*SKIP)(*F)|:', '/', test_string)
    'test/(apple:orange,(orange:apple))/test2'