2016-11-15 62 views
-2

我想將字符串拆分爲3部分。比如我有一個的電子郵件地址一樣VBA正則表達式郵件

[email protected] 

,我想將它拆分成

testuser 
gamil 
.com 

與左,右和中(STR)我如果是固定lenght只能提取字符串。 有沒有人提出一些想法?

+0

HTTP ://stackoverflow.com/documentation/vba/3480/searching-within-strings-for-the-presence-of-substrings#t=20161115192324085467 –

回答

0

leftrightmid(STR)如果是固定長度i僅可以提取的字符串。

這不是真的,因爲你也可以使用len函數來獲取字符串的長度。

Dim L as Integer 
L = Len("[email protected]") 
MsgBox L 

您還可以使用Instr(和InstrRev,逆轉)函數找到特定字符或字符串的索引。

Dim I as Integer 
I = Instr("[email protected]", "@") 

因此,對於你的情況,沒有正則表達式自定義函數將返回的三個項目的數組:

Function SplitEmail(email$) 
'Function returns an array like: 
' {"username", "domain", "tld"} 

Dim Dot As Integer, At As Integer 
Dot = InStrRev(email, ".") 
At = InStr(email, "@") 

Dim ret(1 To 3) As String 

ret(1) = Left(email, At - 1) 
ret(2) = Mid(email, At + 1, Dot - At - 1) 
ret(3) = Mid(email, Dot + 1) 

SplitEmail = ret 

End Function 

來獲取用戶名的一部分,你可以這樣做:

Dim some_email$ 
some_email = "[email protected]" 

MsgBox SplitEmail(some_email)(1)