在 Python 中
dh = '''"Chapter 1 Introduction 1" "#1"
"1.1 Problem Statement and Basic Definitions 2" "#2"
"1.2 Illustrative Examples 4" "#4"
"1.3 Guidelines for Model Construction 26" "#26"
"Exercises 30" "#30"
"Notes and References 34" "#34"'''
pat = re.compile('^(".+?(\d+)" *"#)\\2" *$',re.M)
def zoo(mat):
return '%s%s"' % (mat.group(1),str(int(mat.group(2))+11))
print dh
print
print pat.sub(zoo,dh)
结果
"Chapter 1 Introduction 1" "#1"
"1.1 Problem Statement and Basic Definitions 2" "#2"
"1.2 Illustrative Examples 4" "#4"
"1.3 Guidelines for Model Construction 26" "#26"
"Exercises 30" "#30"
"Notes and References 34" "#34"
"Chapter 1 Introduction 1" "#12"
"1.1 Problem Statement and Basic Definitions 2" "#13"
"1.2 Illustrative Examples 4" "#15"
"1.3 Guidelines for Model Construction 26" "#37"
"Exercises 30" "#41"
"Notes and References 34" "#45"
.
但是从您在其他消息中公开的前面的字符串开始:
eh = '''Chapter 3 Convex Functions 97
3.1 Definitions 98
3.2 Basic Properties 103'''
pat = re.compile('^(.+?(\d+)) *$',re.M)
def zaa(mat):
return '"%s" "%s"' % (mat.group(1),str(int(mat.group(2))+11))
print eh
print
print pat.sub(zaa,eh)
结果
Chapter 3 Convex Functions 97
3.1 Definitions 98
3.2 Basic Properties 103
"Chapter 3 Convex Functions 97" "108"
"3.1 Definitions 98" "109"
"3.2 Basic Properties 103" "114"
所有这些都是功课吗?
.
编辑:
我更正了上面的第一个代码
dh = '''(bookmarks
("Chapter 1 Introduction 1" "#1")
("1.1 Problem Statement and Basic Definitions 2" "#2")
("1.2 Illustrative Examples 4" "#4")
("1.3 Guidelines for Model Construction 26" "#26")
("Exercises 30" "#30")
("Notes and References 34" "#34"))
)'''
pat = re.compile('^(\(".+?(\d+)" *"#)\\2" *(\)\)?)$',re.M)
def zoo(mat):
return '%s%s"%s' % (mat.group(1),str(int(mat.group(2))+11),mat.group(3))
print dh
print
print pat.sub(zoo,dh)
结果
(bookmarks
("Chapter 1 Introduction 1" "#1")
("1.1 Problem Statement and Basic Definitions 2" "#2")
("1.2 Illustrative Examples 4" "#4")
("1.3 Guidelines for Model Construction 26" "#26")
("Exercises 30" "#30")
("Notes and References 34" "#34"))
)
(bookmarks
("Chapter 1 Introduction 1" "#12")
("1.1 Problem Statement and Basic Definitions 2" "#13")
("1.2 Illustrative Examples 4" "#15")
("1.3 Guidelines for Model Construction 26" "#37")
("Exercises 30" "#41")
("Notes and References 34" "#45"))
)