0

2 つの文字列を比較するために使用したいこのコードがあります。アイデアは、単語の最初の文字と数字の最後の 4 文字を取得し、それらを組み合わせて、別の単語と比較できるようにすることです。たとえば、「Smith John 123456」があり、「s3456」と入力したい場合、それを見つけることができるはずです。

Dim strFileName, strTxtValue

strFileName = "4ABCD_Delta_Jhon_T_JR_123456"
strTxtValue = "D3456"

Dim item, items, firstInitial, lastFour, myArray

strFileName = replace(strFileName,"_"," ")
myArray = Split(strFileName)

For Each item In myArray
    If IsNumeric(item) Then
        lastFour = Right(item, Len(item)-2)
        Exit For
    End If
Next
For Each items In myArray
    firstInitial = Left(items, 1)&lastFour
    If UCase(strTxtValue) = UCase(firstInitial) Then
        Contains = True
    End If
Next

これまでのところ、これは私が持っているものですが、機能させることができませんでした。誰か助けてくれませんか?

4

3 に答える 3

1

別の方法:

strFileName = "4ABCD_Delta_Jhon_T_JR_123456"
strTxtValue = "D3456"
strFromName = ""
myArray     = Split(strFileName, "_")
For i = 0 To UBound(myArray)
    firstChar = Asc(myArray(i))
    'if not a number...
    If firstChar < 48 Or firstChar > 57 Then
        strFromName = Chr(firstChar)
        Exit For
    End If
Next
strFromName = strFromName & Right(strFileName, 4)
WScript.Echo strFromName, CStr(strFromName = strTxtValue)
'>> D3456 True
于 2013-04-07T21:29:33.990 に答える
1

別の方法は、正規表現を使用することです。

s = "Smith John 123456"

Set re = New RegExp
re.Pattern = "^(.).*(.{4})$"

WScript.Echo LCase(re.Replace(s, "$1$2"))

2番目の例では、次のようになります。

s = "4ABCD_Delta_Jhon_T_JR_123456"

Set re = New RegExp
re.Pattern = "^(?:\w*?[0-9]\w*?_)?([a-z])[a-z]*_.*(.{4})$"
re.IgnoreCase = True

WScript.Echo LCase(re.Replace(s, "$1$2"))
于 2013-04-07T19:14:53.407 に答える