Forum Discussion
Why does the following pattern work with one of my udf vs the other
Please attach a sample workbook with the code and some dummy data, so that we don't have to do all the work.
- HansVogelaarOct 02, 2021MVP
Thanks!
The getTargetValueV2 function tries to return the first of the submatches of the last match.
A submatch is a value corresponding to an expression between parentheses in the pattern.
The formula in B1 uses "target1 = (\d+)" as pattern. It has one expression in parentheses: (\d+).
The matches collection contains two items: "target1 = 1234" and "target1 = 5678". The numeric strings 1234 and 5678 are the result of the expression (\d+), so they are the submatches.
The formula in B2 uses ^[0-9]{1,3} as pattern. This does not have an expression in parentheses, so there will be no submatches.
You could change the pattern to ^([0-9]{1,3}) so that the formula becomes
=getTargetValueV2(A2,"^([0-9]{1,3})")
The formula will then return 12.