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

如何使用Ruby正则表达式提取指定字符串中的95E10类物料编号?

Ruby正则提取物料编号方案

正则表达式设计

观察两个物料编号的特征:均以95开头,由字母、数字组成独立标识符,因此可以用以下正则匹配:

/\b95[A-Z0-9]+\b/
  • \b:匹配单词边界,避免误匹配包含95的长字符串
  • 95:匹配编号固定前缀
  • [A-Z0-9]+:匹配后续1个及以上大写字母或数字
  • 末尾\b:确保编号为独立单词

代码示例

提取单个编号

# 处理第一类字符串
str1 = "Stop Blasting to Move Track, Continue Blasting 95E10 FF"
if match = str1.match(/\b95[A-Z0-9]+\b/)
  puts match[0] # 输出:95E10
end

# 处理第二类字符串
str2 = "Continue Blasting I.D. on Exchanger 95HE03A"
if match = str2.match(/\b95[A-Z0-9]+\b/)
  puts match[0] # 输出:95HE03A
end

提取所有符合条件的编号(多场景兼容)

如果字符串中存在多个物料编号,可使用scan方法一次性提取:

str1.scan(/\b95[A-Z0-9]+\b/) # 返回:["95E10"]
str2.scan(/\b95[A-Z0-9]+\b/) # 返回:["95HE03A"]

扩展说明

如果物料编号可能包含小写字母,只需将正则调整为:

/\b95[A-Za-z0-9]+\b/

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:40:27