如何捕获整数与其后继者之间的任何内容?例如,假设我们要在b3c
中捕获1
和2
之间的a1b3c2d
。以下代码为我们提供了b3c2d
。
'a1b3c2d' =~ / (\d+) (.+) (?{ $1 + 1 }) /x;
print $2, "\n";
答案 0 :(得分:5)
您正在寻找(??{ ... })
,它将代码块的结果解释为正则表达式,而不是(?{ ... })
,它总是成功作为断言。并且您可能希望在两者之间使用非贪婪的量词,以便它在以下数字的第一个匹配处停止。放在一起,/ (\d+) (.+?) (??{ $1 + 1 }) /x
完成工作。
您可能还希望在代码断言之前和之后添加否定断言,以防止匹配“不完整”整数 - 即您不希望将“21”的第一个数字视为“2”。那会给出
/ (\d+) (.+?) (?<!\d) (??{ $1 + 1 }) (?!\d) /x
共
答案 1 :(得分:3)
该构造中需要两个问号。只需一个,只需将代码的值放入# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py
import site # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py
import os # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
import posixpath # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.py
import stat # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py
import genericpath # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py
import warnings # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.py
import linecache # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.py
import types # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py
import UserDict # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.py
import _abcoll # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py
import abc # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py
import _weakrefset # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.pyc
import _weakref # builtin
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py
import copy_reg # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.py
import traceback # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py
import sysconfig # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py
import re # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.py
import sre_compile # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.pyc
import _sre # builtin
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py
import sre_parse # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.py
import sre_constants # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_constants.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.py
import _sysconfigdata # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.py
import _osx_support # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.pyc
# zipimport: found 135 names in /Library/Python/2.7/site-packages/setuptools-3.5.1-py2.7.egg
import encodings # directory /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/__init__.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/__init__.py
import encodings # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/__init__.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.py
import codecs # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.pyc
import _codecs # builtin
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.py
import encodings.aliases # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc
# /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc matches /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py
import encodings.utf_8 # precompiled from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named pytz
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] encodings
# cleanup[1] site
# cleanup[1] sysconfig
# cleanup[1] abc
# cleanup[1] _weakrefset
# cleanup[1] sre_constants
# cleanup[1] _codecs
# cleanup[1] _warnings
# cleanup[1] zipimport
# cleanup[1] _sysconfigdata
# cleanup[1] encodings.utf_8
# cleanup[1] _osx_support
# cleanup[1] codecs
# cleanup[1] signal
# cleanup[1] traceback
# cleanup[1] posix
# cleanup[1] encodings.aliases
# cleanup[1] exceptions
# cleanup[1] _weakref
# cleanup[1] re
# cleanup[1] sre_compile
# cleanup[1] _sre
# cleanup[1] sre_parse
# cleanup[2] copy_reg
# cleanup[2] posixpath
# cleanup[2] errno
# cleanup[2] _abcoll
# cleanup[2] types
# cleanup[2] genericpath
# cleanup[2] stat
# cleanup[2] warnings
# cleanup[2] UserDict
# cleanup[2] os.path
# cleanup[2] linecache
# cleanup[2] os
# cleanup sys
# cleanup __builtin__
# cleanup ints: 827 unfreed ints
# cleanup floats
并返回匹配
$^R
'a1b3c2d' =~ / (\d+) (.+) (??{ $1 + 1 }) /x;
print $2, "\n";