Retrieve the match text (提取获取的字符)

来源:互联网 发布:数码收纳包 知乎 编辑:程序博客网 时间:2024/06/05 04:07

需求:

获取字符串Do you like 12 or 34?中的12


方法:

1. Python

a.  Global function

import re

match = re.search(r"\d+", subject)
if match:
    result = match.group()
else:
    result = ""


b. Compiled object

import re

reobj = re.compile(r"\d+")
match = reobj.search(subject)
if match:
    result = match.group()
else:
    result = ""


2. Tcl

regexp -linestop {\d+} $subject result

0 0