我需要将版权年份定位在字符串的开头。以下是我可能会有的输入:
(c) 2012 10 DC Comics
2012 DC Comics
10 DC Comics. 2012
10 DC Comics , (c) 2012.
10 DC Comics, Copyright 2012
Warner Bros, 2011
Stanford and Sons, Ltd. Inc. (C) 2011. All Rights Reserved.
...etc...从这些输入中,我需要始终具有相同格式的输出-
2012. 10 DC Comics.
2011. Warner Bros.
2011. Stanford and Sons, Ltd. Inc. All Rights Reserved
etc...我如何使用字符串格式化和正则表达式的组合来完成此操作?
这需要清理,但这就是我目前正在做的事情:
### copyright
copyright = value_from_key(sd_wb, 'COPYRIGHT', n).strip()
m = re.search('[0-2][0-9][0-9][0-9]', copyright)
try:
year = m.group(0)
except AttributeError:
copyright=''
else:
copyright = year + ". " + copyright.replace(year,'')
copyright = copyright.rstrip('.').strip() + '.'
if copyright:
copyright=copyright.replace('\xc2\xa9 ','').replace('&', '&').replace('(c)','').replace('(C)','').replace('Copyright', '')
if not copyright.endswith('.'):
copyright = copyright + '.'
copyright = copyright.replace(' ', ' ')发布于 2012-03-13 21:18:09
此程序:
from __future__ import print_function
import re
tests = (
'(c) 2012 DC Comics',
'DC Comics. 2012',
'DC Comics, (c) 2012.',
'DC Comics, Copyright 2012',
'(c) 2012 10 DC Comics',
'10 DC Comics. 2012',
'10 DC Comics , (c) 2012.',
'10 DC Comics, Copyright 2012',
'Warner Bros, 2011',
'Stanford and Sons, Ltd. Inc. (C) 2011. All Rights Reserved.',
)
for input in tests:
print("<", input)
output = re.sub(r'''
(?P<lead> (?: \S .*? \S )?? )
[\s.,]*
(?: (?: \( c \) | copyright ) \s+ )?
(?P<year> (?:19|20)\d\d )
[\s.,]?
''', r"\g<year>. \g<lead>", input, 1, re.I + re.X)
print(">", output, "\n")在Python 2.7或3.2下运行时,会生成以下输出:
< (c) 2012 DC Comics
> 2012. DC Comics
< DC Comics. 2012
> 2012. DC Comics
< DC Comics, (c) 2012.
> 2012. DC Comics
< DC Comics, Copyright 2012
> 2012. DC Comics
< (c) 2012 10 DC Comics
> 2012. 10 DC Comics
< 10 DC Comics. 2012
> 2012. 10 DC Comics
< 10 DC Comics , (c) 2012.
> 2012. 10 DC Comics
< 10 DC Comics, Copyright 2012
> 2012. 10 DC Comics
< Warner Bros, 2011
> 2011. Warner Bros
< Stanford and Sons, Ltd. Inc. (C) 2011. All Rights Reserved.
> 2011. Stanford and Sons, Ltd. Inc All Rights Reserved. 这似乎就是你要找的。
发布于 2012-03-13 06:33:16
不使用正则表达式的答案如何?
tests = (
'(c) 2012 DC Comics',
'DC Comics. 2012',
'DC Comics, (c) 2012.',
'DC Comics, Copyright 2012',
'(c) 2012 10 DC Comics',
'10 DC Comics. 2012',
'10 DC Comics , (c) 2012.',
'10 DC Comics, Copyright 2012',
'Warner Bros, 2011',
'Stanford and Sons, Ltd. Inc. (C) 2011. All Rights Reserved.',
)
def reorder_copyright(text):
year = None
first = []
second = []
words = text.split()
if words[0].lower() in ('(c)','copyright'):
year = words[1]
company = ' '.join(words[2:])
for i, word in enumerate(words):
if word.lower() in ('(c)','copyright'):
year = words[i+1]
company = ' '.join(words[:i] + words[i+2:])
break
else:
year = words[-1]
company = ' '.join(words[:-1])
year = year.strip(' ,.')
company = company.strip(' ,.')
return "%s. %s." % (year, company)
if __name__ == '__main__':
for line in tests:
print(reorder_copyright(line))发布于 2012-03-13 06:42:58
搜索
^\(c\)\s+(?P<year>\d{4})\s+(?P<digits>\d{2}).*$|^(?P<digits>\d{2}).*(?P<year>\d{4})\.?替换
\g<year>. \g<digits> DC Comics.这适用于任何四位数的年份(不仅仅是2012年)和任何两位数的数字(不仅仅是10位数)。不知道你是否需要它。它太难看了,无法解释:)
编辑:在我发布这个答案之后,OP更改了输入和输出,所以它不会工作。往前走,这里没什么可看的。
https://stackoverflow.com/questions/9675654
复制相似问题