擅长:python、mysql、java
<p>尝试正则表达式:</p>
<pre><code>import re
output = list()
header = 'HGLOABCD8PSGL_ZXFH J20190103NXT_APAC'
#Using the regex pattern '\d*' this will fnid all the numeric sequences in the input string
find_all_numbers = re.findall('\d*', header)
#Filtering out any empty string resulted from extraction
numbers = [num for num in find_all_numbers if len(num)==8]
#Getting the largest number which is most of the time going to be the date in your case
date = numbers[0]
#Unpacking the data using string slicing
year, month, day = date[:4], date[4:6], date[6:]
#Using string format defining the desired format using left 0 padding
current_month, previous_month = '{0:03d}'.format(int(month)), '{0:03d}'.format(int(month)-1)
if previous_month =='000':
previous_month = '012'
output.extend((current_month, previous_month))
print(output)
</code></pre>