Python multiple regex matching in a single statement -


i have large python string shown below.

large_string = "the mit license (mit)\n\ncopyright (c) [year] [fullname]\n\npermission hereby granted, free of charge, person obtaining ...." 

i want replace "[year]" in string current year , "[fullname]" user provided name. using re library in python task.

import re import datetime  def pattern_replace(large_string, name):     year = datetime.now().year     large_string = re.sub('\[year\]', str(year), large_string)     large_string = re.sub('\[fullname\]', name, large_string)     return large_string 

but think it's not appropriate way. have never used regex matching before think there should more pythonic way combine 2 re.sub statements.

the task simpler if 2 regular expressions, rather one.

if combine them one, make more fragile (i.e. dependent on ordering of 2 things), , regular expression needs more complex.

you like:

large_string = re.sub('\[year\](.*?)\[fullname\]', str(year) + '\g<1>' + name, large_string) 

ask find easier read, though. recommend going option.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -