【发布时间】:2020-09-05 20:50:04
【问题描述】:
我们在组织中有一个部门,它在旧应用程序中获得了长度有限的描述字段。他们为各种短语提出了自己的首字母缩略词和缩写词,我们希望将其转换回英文短语。
我希望有一种比我目前正在做的更好的方法来解析这些描述。
示例项目:
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”语句肯定是非常低效的每天编码几百次。有没有更好的方法来使用数组或正则表达式,或者比上述更有效的方法?
【问题讨论】:
-
您应该使用从数据库驱动的查找表,这样会更容易管理并减少代码异味。
-
它们将来自数据库,我只是不想让问题过于复杂,因为担心更多的是使用的代码而不是需要查找的项目的来源。跨度>
-
@Beems 考虑使用
Replace函数。见:stackoverflow.com/a/14973169 -
看看它们是否来自数据库,代码可以是一个/两个内衬,因为每次迭代都是我试图提出的观点。
标签: vbscript asp-classic