You need to enable JavaScript to run this app. 如何编写函数拆分以ABE结尾的结构化字符串块及各行子组件
优惠活动
大模型
产品
解决方案
定价
更多

如何编写函数拆分以ABE结尾的结构化字符串块及各行子组件

字符串块处理实现方案

核心实现思路

1. 块拆分逻辑

所有块均以ABE开头的行作为结束标识,拆分步骤如下:

  • 先将完整输入字符串按换行符分割为单行列表,过滤空行
  • 初始化临时数组收集当前块的行,每遍历到ABE开头的行,就将当前临时数组作为一个完整块存入结果集,随后清空临时数组进入下一个块的收集

2. 行拆分规则

首先将单行内所有空白字符全部删除,再根据行前缀匹配对应的固定长度拆分规则:

  • ABB开头行:拆分长度依次为[1,2,3,1,8,4,6,5],对应ID Type 1~8
  • ABH开头行:拆分长度依次为[1,2,6,7,3,7,3,4,4],对应ID Type 1~9
  • ABTB开头行:拆分长度依次为[1,2,2,4,4,4,8,3,3,3],对应ID Type 1~10
  • ABTC开头行:拆分长度依次为[1,2,3,3,6,4,6,3,3],对应ID Type 1~9
  • ABE开头行:拆分长度依次为[1,2,1,6,5,3],对应ID Type 1~6

3. 存储结构建议

推荐使用「对象数组」结构存储结果,兼顾分组和搜索便捷性:

  • 外层为所有块的数组,每个块为独立对象,包含块唯一标识、行列表两个属性
  • 每行是独立对象,包含行类型、原始无空格内容、子组件对象三个属性,子组件对象以ID Type N为键,对应拆分内容为值

代码实现示例(JavaScript为例)

// 预定义行拆分规则
const LINE_SPLIT_RULES = [
  { prefix: 'ABB', lengths: [1,2,3,1,8,4,6,5] },
  { prefix: 'ABH', lengths: [1,2,6,7,3,7,3,4,4] },
  { prefix: 'ABTB', lengths: [1,2,2,4,4,4,8,3,3,3] },
  { prefix: 'ABTC', lengths: [1,2,3,3,6,4,6,3,3] },
  { prefix: 'ABE', lengths: [1,2,1,6,5,3] }
]

// 入口函数:解析所有块
function parseAllBlocks(rawStr) {
  const allLines = rawStr.split('\n').filter(line => line.trim() !== '')
  const blocks = []
  let currentBlockLines = []

  for (const line of allLines) {
    // 移除行内所有空白字符
    const cleanedLine = line.replace(/\s+/g, '')
    currentBlockLines.push(cleanedLine)
    // 遇到结束标识行则完成当前块解析
    if (cleanedLine.startsWith('ABE')) {
      blocks.push(parseSingleBlock(currentBlockLines))
      currentBlockLines = []
    }
  }
  return blocks
}

// 解析单个块
function parseSingleBlock(blockLines) {
  return {
    blockId: `block_${Date.now()}_${Math.random().toString(36).slice(2,6)}`,
    lines: blockLines.map(line => parseSingleLine(line))
  }
}

// 解析单行
function parseSingleLine(line) {
  const rule = LINE_SPLIT_RULES.find(r => line.startsWith(r.prefix))
  if (!rule) throw new Error(`未匹配到行规则: ${line}`)

  const components = {}
  let offset = 0
  rule.lengths.forEach((len, idx) => {
    components[`ID Type ${idx + 1}`] = line.slice(offset, offset + len)
    offset += len
  })

  return {
    lineType: rule.prefix,
    rawCleanLine: line,
    components
  }
}

使用示例

const testInput = `ABBV08E201902162221     99927781608
ABH5684529845212JB19015037B0984565HCPP
ABTB034T  0084300130310325294     789657
ABTC095C  0084399845285165654     852
ABE05190000000752`
const parseResult = parseAllBlocks(testInput)
console.log(parseResult)

搜索/分组操作示例

按子组件值搜索

// 搜索所有ID Type 2值为BH的行
const matchedLines = []
parseResult.forEach(block => {
  matchedLines.push(...block.lines.filter(line => line.components['ID Type 2'] === 'BH'))
})

按行类型分组

// 按行类型分组所有行
const groupedLines = {}
parseResult.forEach(block => {
  block.lines.forEach(line => {
    if (!groupedLines[line.lineType]) groupedLines[line.lineType] = []
    groupedLines[line.lineType].push(line)
  })
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 18:06:03