cnilsson76 Posted January 28, 2022 Posted January 28, 2022 I need to match a number in a string to a specific list of numbers via StringRegExp. I have successfully built an expression that matches the numbers to a list of possible matches but the two digit numbers are causing problems. #include <MsgBoxConstants.au3> Global $aValues[22] = ["B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11", "B12", "B13", "B14", "B15", "B16", "B17", "B18", "B19", "B20", "B21", "B22", "B23"] For $i = 0 To 21 $iMatch = StringRegExp($aValues[$i], "(3)|(6)|(9)|(12)|(15)|(18)|(21)") $Found = (($iMatch = 1) ? ("Found") : ("Not found") ) MsgBox($MB_SYSTEMMODAL, "Result", $Found & " in:" & @CRLF & $aValues[$i]) Next As you can see, I want to only match if the string contains the literal whole number 3, 6, 9, 12, 15, 18, or 21. I mean the entire number. So three literally means "3". I don't want the number 13 or 23 to trigger a match. So, "B6" should evaluate as a match but "B16" should not. I have tried many variations of the RegEx but the pattern above is the closest I can get. Thank you all for your help!
Deye Posted January 28, 2022 Posted January 28, 2022 (edited) Try this pattern : "\D(3|6|9|12|15|18|21)\b" Edited January 28, 2022 by Deye
cnilsson76 Posted January 28, 2022 Author Posted January 28, 2022 32 minutes ago, Deye said: Try this pattern : "\w(3|6|9|12|15|18|21)$" Thanks for the reply, however, this pattern fails in the same way. It matches "B13", "B16", "B19", and "B23" because they have a 3, 6, or 9 in the number. 3, 6, and 9 are valid matches but I only want them to match if they are the complete number. I want to match "3" but not match "13" or "23".
Solution Deye Posted January 28, 2022 Solution Posted January 28, 2022 Sorry, please try the corrected pattern above
cnilsson76 Posted January 28, 2022 Author Posted January 28, 2022 44 minutes ago, Deye said: Sorry, please try the corrected pattern above Thank you so much. The addition of the \D \b did the trick! Thank you for your help! Correctly functioning code below for any future users searching for this answer. #include <MsgBoxConstants.au3> Global $aValues[22] = ["B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11", "B12", "B13", "B14", "B15", "B16", "B17", "B18", "B19", "B20", "B21", "B22", "B23"] For $i = 0 To 21 $iMatch = StringRegExp($aValues[$i], "\D(3|6|9|12|15|18|21)\b") $Found = (($iMatch = 1) ? ("Found") : ("Not found") ) MsgBox($MB_SYSTEMMODAL, "Result", $Found & " in:" & @CRLF & $aValues[$i]) Next
mikell Posted January 28, 2022 Posted January 28, 2022 Depending on the real purpose checking the match could be an other way #include <MsgBoxConstants.au3> Global $aValues[22] = ["B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11", "B12", "B13", "B14", "B15", "B16", "B17", "B18", "B19", "B20", "B21", "B22", "B23"] For $i = 0 To 21 $Found = Mod(StringRegExp($aValues[$i], '\d+', 1)[0], 3) ? "Not found" : "Found" MsgBox($MB_SYSTEMMODAL, "Result", $Found & " in:" & @CRLF & $aValues[$i]) Next
cnilsson76 Posted January 31, 2022 Author Posted January 31, 2022 On 1/28/2022 at 2:50 AM, mikell said: Depending on the real purpose checking the match could be an other way #include <MsgBoxConstants.au3> Global $aValues[22] = ["B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11", "B12", "B13", "B14", "B15", "B16", "B17", "B18", "B19", "B20", "B21", "B22", "B23"] For $i = 0 To 21 $Found = Mod(StringRegExp($aValues[$i], '\d+', 1)[0], 3) ? "Not found" : "Found" MsgBox($MB_SYSTEMMODAL, "Result", $Found & " in:" & @CRLF & $aValues[$i]) Next This method worked as well...thank you for the help!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now