You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:从给定字符串中提取位数≥4的数字

实现方案:提取位数≥4的数字并输出

思路

从目标字符串中提取所有连续数字片段,筛选出长度≥4的数字,最后将符合条件的数字用空格拼接输出。


Python 实现

import re

# 定义原始字符串列表
target_strings = [
    "TC_11601662 - Group 0: CPU Lockstep alarms to reaction reset and FSP0 activation",
    "TC_100 - Group 1: CPU Lockstep alarms to reaction reset and FSP0 activation",
    "TC_1001 - Group 2: CPU Lockstep alarms to reaction reset and FSP0 activation"
]

# 提取并筛选符合要求的数字
valid_numbers = []
for s in target_strings:
    # 匹配所有连续数字
    all_numbers = re.findall(r'\d+', s)
    # 筛选长度≥4的数字
    valid_numbers.extend([num for num in all_numbers if len(num) >= 4])

# 输出结果
print(f"out: {' '.join(valid_numbers)}")

执行后输出:

out: 11601662 1001

Shell(Bash)实现

# 原始文本内容
input_text="TC_11601662 - Group 0: CPU Lockstep alarms to reaction reset and FSP0 activation
TC_100 - Group 1: CPU Lockstep alarms to reaction reset and FSP0 activation
TC_1001 - Group 2: CPU Lockstep alarms to reaction reset and FSP0 activation"

# 提取并筛选数字,输出结果
echo "out: $(echo "$input_text" | grep -oE '[0-9]+' | awk 'length($0) >= 4' | tr '\n' ' ' | sed 's/ $//')"

执行后输出:

out: 11601662 1001

内容的提问来源于stack exchange,提问作者user7482089

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 23:27:28