Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

excel - How to filter table with RegEx in VBA?

I have a table with column of IP Address, some of them have a mask with CIDR notation (1.1.1.1/16 for example).

I would like to filter that table with a regex expression, I tried it in Regex101 and it is what I need :

"[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/(($[0-9]|[0-9] )|(1[0-9]))"

How can I apply the filter to my table with my regex, only show lines where regex is true ?

question from:https://stackoverflow.com/questions/65919070/how-to-filter-table-with-regex-in-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First check this is in Tools -> references :

enter image description here

Then you should follow this example :

        Dim current_string As String
        Dim my_pattern As String
        my_pattern = "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/(($[0-9]|[0-9] )|(1[0-9]))"
        Dim my_regexp As RegExp
        With my_regexp
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = my_pattern 
        End With
        For i = 1 To N
            current_string = Range("A" & i).Text
            If my_regexp.test(current_string)=False Then
                Range("A" & i).EntireRow.Hidden = True ' we only hide rows that doesn't match the regexp.
            End If
        Next i

Let me know if it helps !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...