Obsession is probably a little strong, but I do like tidy code. You know – proper indentation, a sensible number of comments (which can be more than none but there shouldn’t be as much comment as code) and good names. Hungarian notation brings me out in a rash.
This extends to having keywords, variables and methods in the right case. While in CAL there was a lot of UPPERCASE, in AL there is far more lowercase. It took me a while to get used to but I prefer it now.
If you convert some CAL into AL then likely all the keywords are going to be in uppercase. The code will run fine, it just doesn’t look nice. In the below example my eye is drawn to the fact that some filters are being set, rather than what those filters are – on which records and fields.
You’ll notice that all the UPPERCASE words are highlighted in that example. That’s because they are all search results for my regular expression.
- \b will match a word boundary – spaces, punctuation, start and end of lines – anything that denotes the start or end of a word
- (?!) is a negative lookahead and does not find matches for the expression inside the brackets. This is useful for uppercase words that should be left uppercase like NAV or the AppSource suffix that you’ve added all over the place
- Disclaimer: don’t ask me to explain lookaheads in any more detail than that – I don’t know. I’m not convinced that anyone actually properly knows how regex works 😉
- [A-Z] matches uppercase characters between A and Z
- + indicates that we’re looking for one or more of the previous group i.e. one or more uppercase letters
Altogether it translate to something like: match any word of one or more uppercase letters except where it contains “NAV” or “CACTMN” (the suffix we’re using in this app).
Once you’ve found the matches find and replace is your friend. I love how VS Code gives you a preview of all the replaces that it is going to do. Very useful before you replace “IF” with “if” and realise you’ve also replaced “MODIFY” with “MODifY”.
6 thoughts on “Satisfying Your Case-Sensitive Obsession with Regex”
Actually, if you convert to AL using the Txt2Al tool (https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-txt2al-tool), there will be no UPPERCASE anymore. So how did you convert your code?
Thanks for the comment – interesting. Is that a recent change? Maybe we’ve used an older version of txt2al.
That might be the case indeed. But if you were already using the finsql.exe of BC14 (as indicated in the help article), then I’m of ideas, tbh 😉
I should add that when I say Txt2Al, I mean ExportToNewSyntax as well, I think this is the step that does the LOWERCASE trick and probably was missing in your case.
Will you using it in your build pipeline to block a build ? 😉
Now you’re talking my language 😊