python - 为什么这里的 sub() 函数可以只传两个参数?
PHPz
PHPz 2017-04-18 09:17:31
0
2
457

一个模版系统
这是程序:

#!/usr/bin/env python
# templates.py

import fileinput, re

# Match strings in [ ]
field_pat = re.compile(r'\[(.+?)\]')

# Collect variables
scope = {}

# for re.sub
def replacement(match):
    code = match.group(1)
    try:
        # If string can be evaluate out a value, return it.
        return str(eval(code, scope))
    except SyntaxError:
        # else exec the assignment statement in action scope.
        exec code in scope
        # ...... return empty string
        return ''

# Get all text in a string
# Also, there is another way, consider Chapter 11
lines = []
for line in fileinput.input():
    lines.append(line)
    text = ''.join(lines)

# replace all items match field pattern
print field_pat.sub(replacement, text)

另外这是 strings.txt 文件内的内容:

[x = 2]
[y = 3]
The sum of [x] and [y] is [x + y]

运行结果如下:

The sum of 2 and 3 is 5

我的疑问是:
程序的最后一行

print field_pat.sub(replacement, text)

为何只有两个参数?
根据官方对 re.sub() 的文档,re.sub() 的最少参数是3个。

官方文档

PHPz
PHPz

学习是最好的投资!

reply all(2)
迷茫

还是文档Search and Replace:

Another common task is to find all the matches for a pattern, and replace them with a different string. The sub() method takes a replacement value, which can be either a string or a function, and the string to be processed.

.sub(replacement, string[, count=0])

Returns the string obtained by replacing the leftmost non-overlapping occurrences of the RE in string by the replacement replacement. If the pattern isn’t found, string is returned unchanged.

The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. The default value of 0 means to replace all occurrences.

小葫芦

(Please take it with @selfboot’s recipe)

field_pat.sub(replacement, text) 並不是 re.sub()...

Yes

Python2

  • sub(repl, string, count=0)

    • Identical to the sub() function, using the compiled pattern.

Python2-sub

>>> import re
>>> field_pat = re.compile(r'\[(.+?)\]')
>>> field_pat
<_sre.SRE_Pattern object at 0x7fa09b67fdd8>
>>> type(field_pat)
<type '_sre.SRE_Pattern'>
>>> field_pat.sub
<built-in method sub of _sre.SRE_Pattern object at 0x7fa09b67fdd8>
>>> help(field_pat.sub)
sub(...)
    sub(repl, string[, count = 0]) --> newstring
    Return the string obtained by replacing the leftmost non-overlapping
    occurrences of pattern in string by the replacement repl.

Python3

  • regex.sub(repl, string, count=0)

    • Identical to the sub() function, using the compiled pattern.

Python3-regex.sub

>>> import re
>>> field_pat = re.compile(r'\[(.+?)\]')
>>> field_pat
re.compile(r'\[(.+?)\]', re.UNICODE)
>>> type(field_pat)
_sre.SRE_Pattern
>>> field_pat.sub
<function SRE_Pattern.sub>
>>> help(field_pat.sub)
sub(...) method of _sre.SRE_Pattern instance
    sub(repl, string[, count = 0]) -> newstring.
    Return the string obtained by replacing the leftmost non-overlapping
    occurrences of pattern in string by the replacement repl.

Questions I answered: Python-QA

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template