How to extract the substring between two markers?

前端 未结 18 2280
慢半拍i
慢半拍i 2020-11-22 06:02

Let\'s say I have a string \'gfgfdAAA1234ZZZuijjk\' and I want to extract just the \'1234\' part.

I only know what will be the few characte

相关标签:
18条回答
  • 2020-11-22 06:05

    you can do using just one line of code

    >>> import re
    
    >>> re.findall(r'\d{1,5}','gfgfdAAA1234ZZZuijjk')
    
    >>> ['1234']
    

    result will receive list...

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

    Using regular expressions - documentation for further reference

    import re
    
    text = 'gfgfdAAA1234ZZZuijjk'
    
    m = re.search('AAA(.+?)ZZZ', text)
    if m:
        found = m.group(1)
    
    # found: 1234
    

    or:

    import re
    
    text = 'gfgfdAAA1234ZZZuijjk'
    
    try:
        found = re.search('AAA(.+?)ZZZ', text).group(1)
    except AttributeError:
        # AAA, ZZZ not found in the original string
        found = '' # apply your error handling
    
    # found: 1234
    
    0 讨论(0)
  • 2020-11-22 06:08
    text = 'I want to find a string between two substrings'
    left = 'find a '
    right = 'between two'
    
    print(text[text.index(left)+len(left):text.index(right)])
    

    Gives

    string
    
    0 讨论(0)
  • 2020-11-22 06:10

    In python, extracting substring form string can be done using findall method in regular expression (re) module.

    >>> import re
    >>> s = 'gfgfdAAA1234ZZZuijjk'
    >>> ss = re.findall('AAA(.+)ZZZ', s)
    >>> print ss
    ['1234']
    
    0 讨论(0)
  • 2020-11-22 06:10

    You can find first substring with this function in your code (by character index). Also, you can find what is after a substring.

    def FindSubString(strText, strSubString, Offset=None):
        try:
            Start = strText.find(strSubString)
            if Start == -1:
                return -1 # Not Found
            else:
                if Offset == None:
                    Result = strText[Start+len(strSubString):]
                elif Offset == 0:
                    return Start
                else:
                    AfterSubString = Start+len(strSubString)
                    Result = strText[AfterSubString:AfterSubString + int(Offset)]
                return Result
        except:
            return -1
    
    # Example:
    
    Text = "Thanks for contributing an answer to Stack Overflow!"
    subText = "to"
    
    print("Start of first substring in a text:")
    start = FindSubString(Text, subText, 0)
    print(start); print("")
    
    print("Exact substring in a text:")
    print(Text[start:start+len(subText)]); print("")
    
    print("What is after substring \"%s\"?" %(subText))
    print(FindSubString(Text, subText))
    
    # Your answer:
    
    Text = "gfgfdAAA1234ZZZuijjk"
    subText1 = "AAA"
    subText2 = "ZZZ"
    
    AfterText1 = FindSubString(Text, subText1, 0) + len(subText1)
    BeforText2 = FindSubString(Text, subText2, 0) 
    
    print("\nYour answer:\n%s" %(Text[AfterText1:BeforText2]))
    
    0 讨论(0)
  • 2020-11-22 06:11

    Another way of doing it is using lists (supposing the substring you are looking for is made of numbers, only) :

    string = 'gfgfdAAA1234ZZZuijjk'
    numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    output = []
    
    for char in string:
        if char in numbersList: output.append(char)
    
    print(f"output: {''.join(output)}")
    ### output: 1234
    
    0 讨论(0)
提交回复
热议问题