正则表达式可以在字符串中切换字符吗? [英] Can Regex switch charactors in a string?

查看:22
本文介绍了正则表达式可以在字符串中切换字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的包裹 ID.这个数据库的问题是那个模式是错误的.

I have a large db of parcel ID's. The problem with this db is that pattern is wrong.

0000-22N-6W-01
should be written:
0000-01-22N-6W

Regex 可以做一个切换还是我需要查看另一个 python 程序.

Can Regex do a switch or do I need to look at another python procedure.

推荐答案

替换模式 ^(.{4})(.{4})(.{3})(.{3})\1\4\2\3

import re
input = raw_input()
output = re.sub(r'^(.{4})(.{4})(.{3})(.{3})', r'\1\4\2\3', input)
print output

此处进行测试.

Test it here.

这篇关于正则表达式可以在字符串中切换字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆