小编Dra*_*fly的帖子

使用正则表达式对字符串进行分区

尝试将字符串分成两部分。

#Need to get 'I1234' and 'I56/I78'
name1 = 'I1234/I56/I78'

#Need to get '\I1234 ' and 'I56/I78'
name2 = '\I1234 /I56/I78'      

#Need to get '\I1234 ' and '\I56 /I78'
name3 = '\I1234 /\I56 /I78'

#Need to get '\1234 ' and '\I56 /\I78 '
name4 = '\I1234 /\I56 /\I78 '
Run Code Online (Sandbox Code Playgroud)

我尝试了一下,它起作用了:

pat_a = re.compile(r'(.+)(/)(.+)')

Is there a better way ?

result = re.findall(pat_a, name2[::-1])
Run Code Online (Sandbox Code Playgroud)

编辑

可能有更复杂的字符串,例如:

\I78_[0]/abcd_/efg_ /I1234/I56
Run Code Online (Sandbox Code Playgroud)

python regex

2
推荐指数
1
解决办法
194
查看次数

标签 统计

python ×1

regex ×1