Recently, I was working with a customer who needed dynamic worksheet names in their Excel workbooks. With ExcelWriter this is no problem! Since the input was coming from the end users, I need to perform some validation on the input to make sure it conforms to Excel’s restrictions on what a valid worksheet name can be.
Valid Worksheet Names in Excel
- They cannot exceed 31 characters.
- They cannot contain the following characters: \ / ? * [ ]
- The name cannot be the same as another Worksheet in the Workbook.
- The worksheet name cannot be blank
In this case I am interested in #2 and dealing with invalid characters. I know that Regex is the best option for this–when I’m working with Regex, I often find testing to be a pain. I‘ll open Visual Studio, create a console application and write some code to test my patterns. Just when I was starting to get fed up with all these random testing console applications in my projects directory, I remembered an application called LINQPad that my co-work Sean told me about. LINQPad is a standalone C#/VB/F# scratch pad that can instantly execute code. The output is displayed in a rich format.
So I came up with the following Regex Pattern:
[?\\/*\[\]] |
and wanted to test it. Using LINQPad this was a breeze. I typed my pattern into LINQPad and pressed “Execute,” (the green arrow) which allowed me to see the output.
It worked perfectly.
LINQPad can be used for much more than just Regular Expression testing, though I won’t go into the details here. I encourage you to read about all the features at: http://www.linqpad.net/
Now I can spend more time focusing on my work and less time setting up.