简体   繁体   中英

How to combine three different text patterns For Word Find Wildcards

I'm new to the concept of REGEX and couldn't figure out how to use it properly in my VBA code. I want to extract all strings that have the following formats:

AMT.xxx.xx.xxxxxx
AMT.xxx.xx.xxxxx
AMT.xxx.xxxxxx
AMT.xxx.xx.xxx.xxx

where Xs are numbers.

I tried this line and couldn't get the last two patterns.

 With findRange.Find
         .Text = "AMT.[0-9]{3}.[0-9]{2,}.[0-9]{5,}"

I tried to include the last two patterns using the OR(|) operator but it seems like it's not working.

 With findRange.Find
         "AMT.[0-9]{3}.[0-9]{2,}.[0-9]{5,} | AMT.[0-9]{3}.[0-9]{6} 
          | AMT.[0-9]{3}.[0-9]{2}.[0-9]{3}.[0-9]{3}"

How do I update my code to include all four patterns?

Thanks.

Try this Regex

Matches all the cases that you've mentioned in your question

AMT\\.\\d{3}\\.(?:\\d{2,}\\.?)+

Regex101 Demo

Tell me if its not working...

If you really must use Word Find with wildcard matching, Try this pattern:

AMT\\.[0-9]{3}\\.[0-9]{2}[0-9\\.]{1}[.0-9]{3,7}

It should match the above 4 format but could potentially catch some other format due to its inability to match the later part of the string using zero or more occurrences.

Reference - https://wordmvp.com/FAQs/General/UsingWildcards.htm

It can probably factor down to this

AMT\.\d{3}\.\d{2}(?:\.\d{3}(?:\d{2,3}|\.\d{3})|\d{4})

https://regex101.com/r/WtEvjf/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM