Matching function of a regular expression in excel?

◇◆丶佛笑我妖孽 提交于 2019-12-08 03:06:11

问题


I have several cells in my sheet which contain an ISIN.

Here is an example of an ISIN: DE0006231004

I have created a regular expression which matches the ISIN: ^[a-zA-Z]{2}[0-9]{10}$

I want to match this regex on my cell and give a 1 if it matches otherwise a 0.

Is this possible with a function?


回答1:


The following function will do what you need. It will return either 0 (zero) if string doesn't match or 1 (one) if the string matches to pattern.

Function MatchISIN(ISIN As String)

    Dim regEx As Object
    Set regEx = CreateObject("vbscript.regexp")

    regEx.Pattern = "^[a-zA-Z]{2}[0-9]{10}$"
    regEx.IgnoreCase = True
    regEx.Global = True

    Dim Matches As Object
    Set Matches = regEx.Execute(ISIN)

    MatchISIN = Matches.Count

End Function



回答2:


You could use the built-in Like method;

if "DE0006231009" like "[A-Za-z][A-Za-z]##########" then ...


来源:https://stackoverflow.com/questions/19784587/matching-function-of-a-regular-expression-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!