VBScript / ASP经典-解析和替换字符串中的多个项目

问题描述

我们在组织中有一个部门,该部门在旧版应用程序中获得了一个有限长度的描述字段。他们为各种短语提出了自己的首字母缩写词和缩写,我们希望将其转换回英语短语。

我想有一种比我目前正在做的更好的方式来解析这些描述。

样品:

Part #:         Description
-------------------------------------
10001           17OH 2P 2G TRK 2YR
10002           22OH 2P 3G TRK 1YR

在这些示例描述中,我需要对其进行解析以提取以下内容

17OH = 17 Ohm
22OH = 22 Ohm
2P   = 2 Pole
2G   = 2-Gang
3G   = 3-Gang
TRK  = Track
2YR  = 2-Year Warranty
1YR  = 1-Year Warranty

旧版应用程序是用VBScript / Classic ASP编写的,因此以下将是将其转换为英语的一种示例。

descriptionOUTPUT = ""
descriptionRAW = "17OH 2P 2G TRK 2YR"
if instr(descriptionRAW,"17OH") > 0 then descriptionOUTPUT = descriptionOUTPUT & "17 Ohm "
if instr(descriptionRAW,"22OH") > 0 then descriptionOUTPUT = descriptionOUTPUT & "22 Ohm "
if instr(descriptionRAW,"2P") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2 Pole "
if instr(descriptionRAW,"2G") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2 Gang "
if instr(descriptionRAW,"3G") > 0 then descriptionOUTPUT = descriptionOUTPUT & "3 Gang "
if instr(descriptionRAW,"TRK") > 0 then descriptionOUTPUT = descriptionOUTPUT & "Track "
if instr(descriptionRAW,"2YR") > 0 then descriptionOUTPUT = descriptionOUTPUT & "2-Year Warranty "
if instr(descriptionRAW,"1YR") > 0 then descriptionOUTPUT = descriptionOUTPUT & "1-Year Warranty "
response.write(descriptionOUTPUT)

输出为:

17 Ohm 2 Pole 2 Gang Track 2-Year Warranty

如果我们只需要解析大约十二个项目,那会很好,但是我们可能有数百个项目,而且即使我们只是重复执行“ if instr”语句,效率肯定也非常低下每天运行该代码数百次。是否有更好的方法使用数组或Regex或比上面更有效的方法来做到这一点?

解决方法

一种非常基本的方法是在asp中使用dictionary对象。 要么对它们进行硬编码,要么从数据库中读取它们并进行循环比较。

https://www.w3schools.com/asp/asp_ref_dictionary.asp