【问题标题】:VBScript/ASP Classic - Parse and Replace Multiple Items from StringVBScript/ASP Classic - 解析和替换字符串中的多个项目
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我会调查一下,谢谢。你能链接任何适用于这个场景的好例子吗?
  • 如果您已经有记录集,为什么还要添加字典?
  • 正如我所说,要么对它们进行硬编码(如原版),要么从数据库中读取。
【解决方案2】:

回答这个问题有点晚,但如果你还在寻找,使用替换命令相当简单:

descriptionOUTPUT = replace(replace(replace(descriptionRAW,"OH"," ohm"),"P"," Pole"),"G","-Gang")
response.write(descriptionOUTPUT)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2011-06-23
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多