将字符串拆分为特定子字符串

时间:2015-02-19 06:39:26

标签: vba ms-word word-vba

我有一个参考清单。我必须在参考列表中将作者姓名与引用作者进行比较。我必须将字符串拆分到年份,所以我只能获得作者姓名,但我不知道如何继续进行。

例如,给出以下文字:

  

Sperrin,M.,Jaki,T。,& Wit,E。(2010)。贝叶斯混合模型中标签切换问题的概率重新标记策略。计算统计,20,357-366。

我必须提取作者姓名:

Dim txt As String
Dim txtarray() As String
txt = "Sperrin, M., Jaki, T., & Wit, E. (2010). Probabilistic relabelling strategies for the label switching problem in Bayesian mixture models. Statistics in Computing, 20, 357-366."
txtarray = Split(txt, ",")

//Split will split the entire text, and I need the text only until the year

预期输出:txtarray = ("Sperrin", " M.", " Jaki", " T."," & Wit")

1 个答案:

答案 0 :(得分:1)

试试这个:

Sub GetAuthors()
    Dim txt As String, authors As String, authorsArray() As String

    txt = "Sperrin, M., Jaki, T., & Wit, E. (2010). Probabilistic relabelling strategies for the label switching problem in Bayesian mixture models. Statistics in Computing, 20, 357-366."
    authors = VBA.Trim(VBA.Mid$(txt, 1, InStr(1, txt, "(", vbTextCompare) - 1))
    authorsArray = VBA.Split(authors, ",")
End Sub

基本上,这会搜索第一个(以查找日期开始的位置,仅创建作者的子字符串,并将其拆分为,

显然,这仅在日期存在且格式为(YYYY)

时才有效