Regular expression to check Excel cell

I am working on a php application where the user needs to insert an Excel cell ID (e.g. A1 or AB32 ), a value that is stored in the database for later use, and I try to validate the cell ID format with a regular expression, but it just doesn't work , this is what I still have.

 ^[a-zA-Z]\d$ 
+1
source share
2 answers

There is a wonderful answer on @BartKiers on this question, where he builds a function to create these types of regular expressions that must match ranges from x to y. Its logic is perfectly ported to text ranges and tested on the PCRE dialect at regex101.com.

Regular expression:

^(?:[AZ]|[AZ][AZ]|[AX][AF][AD])(?:[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9]|10[0-3][0-9][0-9][0-9][0-9]|104[0-7][0-9][0-9][0-9]|1048[0-4][0-9][0-9]|10485[0-6][0-9]|104857[0-6])$

Which basically says:

  • Part of the columns: AZ or AZ with AZ or AX with AF with AD

  • String: 1-9 or 1-9 with 0-9 or 1-9 with 1-9 with 0-9 , etc. up to a maximum of 104857 s 0-6

It corresponds to the following:

 A1 AA11 AAA111 ZZ12 YY1048575 XFD1048576 

It will not match the following:

 A0 AA01 AAZ1111111 XFD1048577 XFE1048576 ZZZ333 ZZZ9999999 

Here is the chart:

enter image description here

+1
source

try ^[a-zA-Z]{1,4}(\d+)$

  • ^ ([a-zA-Z] +): 1 to 4 letters
  • (\ d +) $: ends with one digit or more
0
source

Source: https://habr.com/ru/post/892848/


All Articles