Shall this Regex do what I expect from it, that is, matching against "A1:B10,C3,D4:E1000"?

Posted by Will Marcouiller on Stack Overflow See other posts from Stack Overflow or by Will Marcouiller
Published on 2011-03-09T16:19:40Z Indexed on 2011/03/11 8:10 UTC
Read the original article Hit count: 288

Filed under:
|
|
|

I'm currently writing a library where I wish to allow the user to be able to specify spreadsheet cell(s) under four possible alternatives:

  1. A single cell: "A1";
  2. Multiple contiguous cells: "A1:B10"
  3. Multiple separate cells: "A1,B6,I60,AA2"
  4. A mix of 2 and 3: "B2:B12,C13:C18,D4,E11000"

Then, to validate whether the input respects these formats, I intended to use a regular expression to match against. I have consulted this article on Wikipedia:
Regular Expression (Wikipedia)

And I also found this related SO question:
regex matching alpha character followed by 4 alphanumerics.

Based on the information provided within the above-linked articles, I would try with this Regex:

Default Readonly Property Cells(ByVal cellsAddresses As String) As ReadOnlyDictionary(Of String, ICell)
    Get
        Dim validAddresses As Regex = New Regex("A-Za-z0-9:,A-Za-z0-9")

        If (Not validAddresses.IsMatch(cellsAddresses)) then _
            Throw New FormatException("cellsAddresses")

        // Proceed with getting the cells from the Interop here...  
    End Get
End Property

Questions

1. Is my regular expression correct? If not, please help me understand what expression I could use.

2. What exception is more likely to be the more meaningful between a FormatException and an InvalidExpressionException? I hesitate here, since it is related to the format under which the property expect the cells to be input, aside, I'm using an (regular) expression to match against.

Thank you kindly for your help and support! =)

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET