我有一个字符串如下
strng ="Fiscal Year Ended March 31, 2018 Total Year (in $000's)"
如果上面的字符串有一个年份子字符串(例如。。20142015等),将“年”子串和其他子串分开。你知道吗
为了得到我用的“年”
re.findall(r"\b20[012]\d\b",strng)
我怎样才能得到子串的其余部分。 预期输出为
year_substring --> '2018'
rest --> 'Fiscal Year Ended March 31, Total Year (in $000's)'
有没有办法用regex把这两个都用上?你知道吗
您可以捕获3个部分,在年、年和其他部分之前串起来,然后在第1组和第3组中连接以获取其余部分:
参见Python demo。输出:
如果您的字符串有多个匹配项,请在模式中使用
re.split
:见another Python demo。你知道吗
您也可以使用
strip()
从前导/尾随空格中去除组。你知道吗相关问题 更多 >
编程相关推荐