How to extract the substring between two markers?

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

    With sed it is possible to do something like this with a string:

    echo "$STRING" | sed -e "s|.*AAA\(.*\)ZZZ.*|\1|"

    And this will give me 1234 as a result.

    You could do the same with re.sub function using the same regex.

    >>> re.sub(r'.*AAA(.*)ZZZ.*', r'\1', 'gfgfdAAA1234ZZZuijjk')
    '1234'
    

    In basic sed, capturing group are represented by \(..\), but in python it was represented by (..).

提交回复
热议问题