【发布时间】:2012-12-18 16:02:44
【问题描述】:
我想将第一次出现的日期或一般的正则表达式带到我的文本开头:
示例:
"I went out on 1 sep 2012 and it was better than 15 jan 2012"
我想得到
"1 sep 2012, I went out on and it was better than 15 jan 2012"
我正在考虑用",1 sep 2012," 替换"1 sep 2012",然后从"," 中删除字符串,但我不知道要写什么来代替replace_with:
line = re.sub(r'\d+\s(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}', 'replace_with', line, 1)
有什么帮助吗?
【问题讨论】:
-
你使用什么语言?
-
您没有使用足够的捕获组...
-
暂时忘记 Python。请查看一些正则表达式教程。你在上面
re.sub()中的第一个参数不会得到你想要的。 -
虽然python re doc很好docs.python.org/2/library/re.html
标签: python regex replace location