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

一个模版系统
这是程序:

#!/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

学习是最好的投资!

membalas semua(2)
迷茫

还是文档Cari dan Ganti:

Satu lagi tugas biasa ialah mencari semua padanan untuk corak dan menggantikannya dengan rentetan yang berbeza. Kaedah sub() mengambil nilai gantian, yang boleh sama ada rentetan atau fungsi dan rentetan untuk diproses.

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

Mengembalikan rentetan yang diperoleh dengan menggantikan kejadian tidak bertindih paling kiri bagi RE dalam rentetan dengan penggantian gantian. Jika corak tidak ditemui, rentetan dikembalikan tidak berubah.

Kiraan hujah pilihan ialah bilangan maksimum kejadian corak untuk diganti; kiraan mestilah integer bukan negatif. Nilai lalai 0 bermaksud menggantikan semua kejadian.

小葫芦

(Sila ambil dengan resipi @selfboot)

field_pat.sub(replacement, text) bukan re.sub()...

Ya

Python2

  • sub(repl, string, count=0)

    • Sama dengan fungsi sub(), menggunakan corak yang disusun.

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)

    • Sama dengan fungsi sub(), menggunakan corak yang disusun.

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.

Soalan yang saya jawab: Python-QA

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan