Microsoft office Access `LIKE` VS `RegEx`

余生长醉 提交于 2019-11-26 11:22:03

问题


I have been having trouble with the Access key term LIKE and it\'s use. I want to use the following RegEx (Regular Expression) in query form as a sort of \"verfication rule\" where the LIKE operator filters my results:

\"^[0]{1}[0-9]{8,9}$\"

How can this be accomplished?


回答1:


I don't think Access allows regex matches (except in VBA, but that's not what you're asking). The LIKE operator doesn't even support alternation.

Therefore you need to split it up into two expressions.

... WHERE (Blah LIKE "0#########") OR (Blah LIKE "0########")

(# means "a single digit" in Access).




回答2:


I know you were not asking about the VBA, but it maybe you will give it a chance

If you open a VBA project, insert new module, then pick Tools -> References and add a reference to Microsoft VBScript Regular Expressions 5.5. Given that pate the code below to the newly inserted module.

Function my_regexp(ByRef sIn As String, ByVal mypattern As String) As String
   Dim r As New RegExp
    Dim colMatches As MatchCollection
    With r
        .Pattern = mypattern
        .IgnoreCase = True
        .Global = False
        .MultiLine = False
        Set colMatches = .Execute(sIn)
    End With
    If colMatches.Count > 0 Then
        my_regexp = colMatches(0).Value
    Else
        my_regexp = ""
    End If
End Function

Now you may use the function above in your SQL queries. So your question would be now solved by invoking

SELECT my_regexp(some_variable, "^[0]{1}[0-9]{8,9}$") FROM some_table

if will return empty string if nothing is matched.

Hope you liked it.



来源:https://stackoverflow.com/questions/5539141/microsoft-office-access-like-vs-regex

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