使用正则表达式从文本中提取数字 - Python 示例
使用正则表达式\s+(\d+)$可以从文本数据中提取出数字6644。
具体代码如下:
import re
data = "\nTCP\t127.0.0.1:5037\t\t0.0.0.0:0\t\t\tLISTENING\t\t6644\nTCP\t127.0.0.1:5037\t\t127.0.0.1:5589\t\tTIME_WAIT\t\t0\nTCP\t127.0.0.1:5037\t\t127.0.0.1:5590\t\tTIME_WAIT\t\t0\nTCP\t127.0.0.1:5037\t\t127.0.0.1:5593\t\tTIME_WAIT\t\t0\nTCP\t127.0.0.1:5037\t\t127.0.0.1:5594\t\tTIME_WAIT\t\t0\nTCP\t127.0.0.1:5037\t\t127.0.0.1:5598\t\tTIME_WAIT\t\t0\nTCP\t127.0.0.1:5037\t\t127.0.0.1:5599\t\tTIME_WAIT\t\t0\n"
pattern = r"\s+(\d+)$"
matches = re.findall(pattern, data, re.MULTILINE)
if matches:
\tprint(matches[0])
else:
\tprint("No match found.")
输出结果为:
6644
原文地址: https://www.cveoy.top/t/topic/pNdA 著作权归作者所有。请勿转载和采集!