简体   繁体   English

"Python - 如何使用 slice 方法、find 和 rfind 在 python 上删除多个字符"

[英]Python - How do i delete more than one character on python using slice method, find and rfind

I need some help in removing two sets of curly brackets with string.Right now it's only removing one set.我需要一些帮助来删除两组带有字符串的大括号。现在它只删除一组。 I tired to use find and rfind to find the first and last occurence of the string index and using slice method to cut out the curly brackets but it can only remove one set of it.我厌倦了使用 find 和 rfind 来查找字符串索引的第一次和最后一次出现,并使用 slice 方法剪切大括号,但它只能删除一组。

It should work something like that它应该像那样工作

Input:<\/strong> {fefeef}this{feofeow}.4849输入:<\/strong> {fefeef}this{feofeow}.4849

Output<\/strong> this.4849输出<\/strong>this.4849

Here is my code这是我的代码

 def help_mmtf():
    
       while True:
    
           user_input = input("Filename?")
           text_input = str(user_input)
    
           if (text_input !=""):
               
    
                word = text_input
                
                index1 = word.find("{")
                index2 = word.find("}")
                print(index1) 
                print(index2)
    
            
                index3 = word.rfind("{")
                index4 = word.rfind("}")
    
                print(index3)
                print(index4)
               
    
                splice_input = text_input[:index1] + text_input[index4+1:]
                print(f'"{splice_input}"')
    
      else:
               
                
               break
            

Use sub function from re module:使用re模块中的sub

import re

s = "{fefeef}this{feofeow}.4849"
out = re.sub(r'{[^}]*}', '', s)

Output:输出:

>>> out
'this.4849'

For more explanation: Regex101更多解释: Regex101

You can use sub<\/code>您可以使用sub<\/code>

import re


def help_mmtf():
    while True:

        user_input = input("Filename?")
        text_input = str(user_input)

        if (text_input != ""):
            word = text_input



            splice_input = re.sub("\{.*?\}", "", word)
            print(f'"{splice_input}"')

        else:


         break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何替换字符串中的多个字符 Python - How do I replace more than one character in a string Python Python rfind包含找到的字符 - Python rfind includes found character 如何在字符串中找到多于一个子字符串的位置(Python 3.4.3 shell) - How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell) 在python中,如何使用for循环查找在句子中出现多次的单词的位置? - In python, how do I find the positions of a word that occurs more than once in a sentence using a for loop? 为什么 rfind 和 find 在 Python 2.6.5 中返回相同的值? - Why do rfind and find return the same values in Python 2.6.5? Python: string.rfind() 有任何空白字符? - Python: string.rfind() with any whitespace character? python如何用多个字符分割字符串? - python how to split string with more than one character? 如何从 Python 中的多个来源生成随机字符? - How to generate random character from more than one source in Python? 使用 Python 点击,如何添加超过 5 个选项,即超过 5 个? - Using Python Click, how do I add more than 5 options i.e. more than 5? 如何在Python中从字符串中查找多个子字符串 - How to find more than one substring from a string in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM