How to extract the substring between two markers?

前端 未结 18 2353
慢半拍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:13

    Typescript. Gets string in between two other strings.

    Searches shortest string between prefixes and postfixes

    prefixes - string / array of strings / null (means search from the start).

    postfixes - string / array of strings / null (means search until the end).

    public getStringInBetween(str: string, prefixes: string | string[] | null,
                              postfixes: string | string[] | null): string {
    
        if (typeof prefixes === 'string') {
            prefixes = [prefixes];
        }
    
        if (typeof postfixes === 'string') {
            postfixes = [postfixes];
        }
    
        if (!str || str.length < 1) {
            throw new Error(str + ' should contain ' + prefixes);
        }
    
        let start = prefixes === null ? { pos: 0, sub: '' } : this.indexOf(str, prefixes);
        const end = postfixes === null ? { pos: str.length, sub: '' } : this.indexOf(str, postfixes, start.pos + start.sub.length);
    
        let value = str.substring(start.pos + start.sub.length, end.pos);
        if (!value || value.length < 1) {
            throw new Error(str + ' should contain string in between ' + prefixes + ' and ' + postfixes);
        }
    
        while (true) {
            try {
                start = this.indexOf(value, prefixes);
            } catch (e) {
                break;
            }
            value = value.substring(start.pos + start.sub.length);
            if (!value || value.length < 1) {
                throw new Error(str + ' should contain string in between ' + prefixes + ' and ' + postfixes);
            }
        }
    
        return value;
    }
    
    0 讨论(0)
  • 2020-11-22 06:14
    >>> s = 'gfgfdAAA1234ZZZuijjk'
    >>> start = s.find('AAA') + 3
    >>> end = s.find('ZZZ', start)
    >>> s[start:end]
    '1234'
    

    Then you can use regexps with the re module as well, if you want, but that's not necessary in your case.

    0 讨论(0)
  • 2020-11-22 06:14
    import re
    print re.search('AAA(.*?)ZZZ', 'gfgfdAAA1234ZZZuijjk').group(1)
    
    0 讨论(0)
  • 2020-11-22 06:14

    You can use re module for that:

    >>> import re
    >>> re.compile(".*AAA(.*)ZZZ.*").match("gfgfdAAA1234ZZZuijjk").groups()
    ('1234,)
    
    0 讨论(0)
  • 2020-11-22 06:17

    regular expression

    import re
    
    re.search(r"(?<=AAA).*?(?=ZZZ)", your_text).group(0)
    

    The above as-is will fail with an AttributeError if there are no "AAA" and "ZZZ" in your_text

    string methods

    your_text.partition("AAA")[2].partition("ZZZ")[0]
    

    The above will return an empty string if either "AAA" or "ZZZ" don't exist in your_text.

    PS Python Challenge?

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

    Using PyParsing

    import pyparsing as pp
    
    word = pp.Word(pp.alphanums)
    
    s = 'gfgfdAAA1234ZZZuijjk'
    rule = pp.nestedExpr('AAA', 'ZZZ')
    for match in rule.searchString(s):
        print(match)
    

    which yields:

    [['1234']]

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