我需要根据 pdf 文件规范匹配名称对象。但是,名称可能包含十六进制数字(以# 开头)来指定特殊字符。我想将这些匹配翻译成相应的字符。有没有一种巧妙的方法可以在不重新解析匹配字符串的情况下做到这一点?
import re
Name = re.compile(r'''
(/ # Literal "/"
(?: #
(?:\#[A-Fa-f0-9]{2}) # Hex numbers
| #
[^\x00-\x20 \x23 \x2f \x7e-\xff] # Other
)+ #
) #
''', re.VERBOSE)
# some examples
names = """
The following are examples of valid literal names:
Raw string Translation
1. /Adobe#20Green -> "Adobe Green"
2. /PANTONE#205757#20CV -> "PANTONE 5757 CV"
3. /paired#28#29parentheses -> "paired( )parentheses"
4. /The_Key_of_F#23_Minor -> "The_Key_of_F#_Minor"
5. /A#42 -> "AB"
6. /Name1
7. /ASomewhatLongerName
8. /A;Name_With-Various***Characters?
9. /1.2
10. /$$
11. /@pattern
12. /.notdef
"""