[Meta] Regex to detect common PS code snippets

So, fellas, here’s a challenge for you more seasoned folks. I have some ideas, but I figured I’d ask around.

I’m sure any regular user here has seen /u/Lee_Dailey‘s fantastic code-formatting guide that he copies about quite a bit to help out those of us newer to Reddit’s Markdown formatting. I want to see if we can put together a basic Automoderator rule that will basically do just that, to save him the work.

Below is the automoderator code one of the kind mods from /r/Excel gave me that they use to detect mis-formatted VB code snippets:

type: any body (includes, regex): '(?m)^b(Sub|Function)bsw*(' moderators_exempt: false comment: | Your VBA code has not not been formatted properly. 

Basically, it just looks at the start of every paragraph of a post, and if it contains certain keywords (for VB, almost all code snippets start with Function orSub) that* don’*t have the proper 4 spaces in front of them for the Markdown formatter to recognise, which are also followed by another word it posts a comment.

This is pretty adaptable, and we could save Lee a fair bit of copy-pasting if we can automate this. After all, we are /r/PowerShell; if we can’t automate it, God save us all! 😉

Now, naturally function is a very common keyword, that’s top of the list. I’m thinking we could also look for the usual Verb-Noun patterns that many cmdlets and functions do follow, and then beyond that perhaps looking for patterns of parameters as well, maybe param( ), and maybe a few other things.

So… yep. I’m OK at regex, could probably put a basic one together, but I know we have a few true regex wizards hanging about here and there, so if you folks could take a few moments and see what you come up with, I’m sure we could have a pretty good solution put together for this.

(And Lee, it may be easier to do if we have the Markdown source for that helpful comment you’ve got saved!)

submitted by /u/Ta11ow
[link] [comments]

Leave a Reply