如何在UiPath中保留首个Invoice Type并删除其余Invoice相关重复项?
字符串处理解决方案
现有字符串:
Invoice Type|Vanula|Invoice|December| Invoice to be submitted according to Invoice type-priority|2022
需求:保留第一个固定的Invoice Type,删除或替换所有其他出现的Invoice或Invoice type实例,其余内容为动态
核心思路
先拆分字符串,把开头的Invoice Type单独保留,对剩下的部分批量删除所有匹配的Invoice或Invoice type(包含大小写变体),最后拼接结果。
具体实现
Python 版本
original_str = "Invoice Type|Vanula|Invoice|December| Invoice to be submitted according to Invoice type-priority|2022" first_key = "Invoice Type" # 定位第一个目标字符串的结束位置 split_idx = original_str.find(first_key) + len(first_key) prefix = original_str[:split_idx] suffix = original_str[split_idx:] # 正则匹配并删除所有"Invoice"或"Invoice type",处理多余空格 import re processed_suffix = re.sub(r'(?i)\binvoice(?: type)?\b', '', suffix) # 清理连续空格和多余分隔符 processed_suffix = re.sub(r'\s+', ' ', processed_suffix).replace('| |', '|').strip() result = f"{prefix}{processed_suffix}" if processed_suffix else prefix print(result)输出:
Invoice Type|Vanula||December| to be submitted according to -priority|2022
注:如果需要移除空的分隔符||,可以再加一行processed_suffix = re.sub(r'\|{2,}', '|', processed_suffix)JavaScript 版本
const originalStr = "Invoice Type|Vanula|Invoice|December| Invoice to be submitted according to Invoice type-priority|2022"; const firstKey = "Invoice Type"; const splitIdx = originalStr.indexOf(firstKey) + firstKey.length; const prefix = originalStr.slice(0, splitIdx); let suffix = originalStr.slice(splitIdx); // 匹配删除目标内容,清理空格和分隔符 suffix = suffix.replace(/\binvoice(?: type)?\b/gi, '') .replace(/\s+/g, ' ') .replace(/\| +\|/g, '||') .trim(); const result = suffix ? `${prefix}${suffix}` : prefix; console.log(result);
内容的提问来源于stack exchange,提问作者Eddie
相关产品推荐
相关产品推荐

