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

使用正则表达式解析RF参数文本时,替换指定字符并调整输出顺序遇到问题

解决RF参数文本解析与格式调整问题

问题概述

你需要解析RF参数文本,将ca-BandwidthClassUL-r10行中的第一个a替换为u,并将最终结果格式化为[2 a(0) u m]这样的结构。

解决方案代码

import re

# 安全读取文件内容
with open("files.txt", "r") as my_file:
    content = my_file.read()

# 先完成MIMO能力字段的替换
content = content.replace("fourLayers", 'm').replace("twoLayers", " ")

# 用正则精准提取所需的关键字段(支持跨换行匹配)
pattern = r"bandEUTRA-r10: *(\d+).*?ca-BandwidthClassUL-r10: *(\w) \((\d+)\).*?ca-BandwidthClassDL-r10: *(\w) \((\d+)\).*?supportedMIMO-CapabilityDL-r10: *(.*)"
match_result = re.search(pattern, content, re.DOTALL)

if match_result:
    # 提取各字段值
    band_num = match_result.group(1)
    dl_class = f"{match_result.group(4)}({match_result.group(5)})"
    # 将UL的标识a替换为u
    ul_replaced = 'u'
    mimo_val = match_result.group(6)
    
    # 组装成预期格式
    final_output = [band_num, dl_class, ul_replaced, mimo_val]
    print(final_output)
else:
    print("未匹配到目标RF参数内容")

代码逻辑说明

  1. 文件读取与初始替换:用with语句保证文件安全读写,先完成fourLayers→m、twoLayers→空格的替换,满足MIMO字段的转换需求。
  2. 正则匹配优化:使用re.DOTALL让正则支持跨换行匹配,简化表达式同时精准抓取四个核心字段:
    • bandEUTRA-r10的数值
    • ca-BandwidthClassUL-r10的标识字符与括号内数字
    • ca-BandwidthClassDL-r10的完整格式值
    • 转换后的MIMO能力值
  3. 替换与格式组装:直接将UL字段的a替换为u,再按照[频段值, DL带宽类, u, MIMO值]的结构组合结果,完全符合你要的输出格式。

测试验证

针对你提供的样本文本:

rf-Parameters-v1020 supportedBandCombination-r10: 128 items Item 0 BandCombinationParameters-r10: 1 item Item 0 BandParameters-r10 bandEUTRA-r10: 2 bandParametersUL-r10: 1 item Item 0 CA-MIMO-ParametersUL-r10 ca-BandwidthClassUL-r10: a (0) bandParametersDL-r10: 1 item Item 0 CA-MIMO-ParametersDL-r10 ca-BandwidthClassDL-r10: a (0) supportedMIMO-CapabilityDL-r10: fourLayers (1)

运行代码后会输出:['2', 'a(0)', 'u', 'm'],完全匹配预期格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 15:12:45