Skip to content

Instantly share code, notes, and snippets.

@kionay
Last active March 26, 2023 00:47
Show Gist options
  • Save kionay/2521a2ac744104266ef72cbf1fd4fb95 to your computer and use it in GitHub Desktop.
Save kionay/2521a2ac744104266ef72cbf1fd4fb95 to your computer and use it in GitHub Desktop.
Regular Expression for matching spreadsheet cells with A1 notation
^(?:(?'startcolumn'[A-Za-z]{1,3})(?'startrow'\d{1,7}):(?'endcolumn'[A-Za-z]{1,3})(?'endrow'\d{1,7}))$|^(?:(?'startcolumn'[A-Za-z]{1,3}):(?'endcolumn'[A-Za-z]{1,3}))$|^(?:(?'startcolumn'[A-Za-z]{1,3})(?'startrow'\d{1,7}))$|^(?:(?'startcolumn'[A-Za-z]{1,3})(?'startrow'\d{1,7}):(?'endcolumn'[A-Za-z]{1,3}))$|^(?:(?'startcolumn'[A-Za-z]{1,3})(?'startrow'\d{1,7}):(?'endrow'\d{1,7}))$|^(?:(?'startcolumn'[A-Za-z]{1,3}):(?'endcolumn'[A-Za-z]{1,3})(?'endrow'\d{1,7}))$|^(?:(?'startrow'\d{1,7}):(?'endrow'\d{1,7}))$|^(?:(?'startrow'\d{1,7}):(?'endcolumn'[A-Za-z]{1,3})(?'endrow'\d{1,7}))$
@kionay
Copy link
Author

kionay commented Mar 26, 2023

Uses overlapping named capture groups so it only works with in C# with .NET's regex engine.
This is actually 8 regular expressions OR'd together, each one identifying a different format.
A1:B2
A:B
A1
A1:B
A1:2
A:B2
1:2
1:B2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment