我使用GSKinner's Reg Exr tool來幫助提出一種模式,可以在包含大量其他垃圾的字段中查找授權號碼。授權號碼是一個包含字母(有時),數字(總是)和連字符(有時)的字符串(,即授權始終包含某處的數字,但不總是包含連字符和字母)。此外,授權號碼可以位於我正在搜索的字段中的任何位置。適當的授權號碼的正則表達式模式提取授權號碼
實例包括:
5555834384734 ' All digits
12110-AANM ' Alpha plus digits, plus hyphens
R-455545-AB-9 ' Alpha plus digits, plus multiple hyphens
R-45-54A-AB-9 ' Alpha plus digits, plus multiple hyphens
W892160 ' Alpha plus digits without hypens
下面是與附加的垃圾,有時附加到以連字符或沒有空間的真實的授權號碼一些示例數據,使它看起來像的部分數。垃圾以可預測的形式/單詞出現:REF,CHEST,IP,AMB,OBV和HOLD不屬於授權號碼的一部分。
5557653700 IP
R025257413-001
REF 120407175
SNK601M71016
U0504124 AMB
W892160
019870270000000
00Q926K2
A025229563
01615217 AMB
12042-0148
SNK601M71016
12096NHP174
12100-ACDE
12110-AANM
12114AD5QIP
REF-34555
3681869/OBV ONL
下面是我使用的模式:我正在學習正則表達式,因此毫無疑問可以提高
"\b[a-zA-Z]*[\d]+[-]*[\d]*[A-Za-z0-9]*[\b]*"
,但它適用於上述情況,只是不適合以下情況:
REFA5-208-4990IP 'Extract the string 'A5-208-4990'without REF or IP
OBV1213110379 'Extract the string '1213110379' without the OBV
5520849900AMB 'Extract the string '5520849900' without AMB
5520849900CHEST 'Extract the string '5520849900' without CHEST
5520849900-IP 'Extract the string '5520849900' without -IP
1205310691-OBV 'Extract the string without the -OBV
R-025257413-001 'Numbers of this form should also be allowed.
NO PCT 93660 'If string contains the word NO anywhere, it is not a match
HOLDA5-208-4990 'If string contains the word HOLD anywhere, it is not a match
有人可以幫忙嗎?
出於測試目的,這裏的子與樣本輸入數據創建一個表:
Sub CreateTestAuth()
Dim dbs As Database
Set dbs = CurrentDb
With dbs
.Execute "CREATE TABLE tbl_test_auth " _
& "(AUTHSTR CHAR);"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('5557653700 IP');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "(' R025257413-001');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('REF 120407175');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('SNK601M71016');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('U0504124 AMB');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('3681869/OBV ONL');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('REFA5-208-4990IP');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('5520849900AMB');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('5520849900CHEST');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('5520849900-IP');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('1205310691-OBV');"
.Execute " INSERT INTO tbl_test_auth " _
& "(AUTHSTR) VALUES " _
& "('HOLDA5-208-4990');"
.Close
End With
End Sub
開始處的'\ b'似乎沒問題,因爲示例中的第一個字符總是字母或數字。最後的'[\ b]'是不正確的(它匹配退格字符,而不是字邊界),但'*'使它成爲可選的,所以它根本沒有任何作用。另外,你的'[a-zA-Z | \ s | - ]'應該只是'[a-zA-Z \ s-]'; 「or」在字符類中是自動的,所以'|'匹配一個字符'|'。 –