用于验证Excel单元格的正则表达式 [英] Regular expression to validate Excel cell
问题描述
我正在使用一个php应用程序,用户必须插入Excel的单元格ID(例如 A1
或 AB32
),一个存储在数据库中以供将来使用的值,我试图使用正则表达式验证单元格ID格式,但它似乎不起作用,这就是我所得到的到目前为止。
^ [a-zA-Z] \d $
有一个很棒的
I'm working on a php application where the user has to insert an Excel's cell id (for example A1
or AB32
), a value which is stored in the database for later use and I'm trying to validate the cell id format using a regular expression, but it just doesn't seem to be working, this is what I've got so far.
^[a-zA-Z]\d$
There's an awesome answer in this question by @BartKiers where he builds a function to construct these type of regexes that need to match ranges of x to y. His logic transfers nicely to text ranges and is tested in PCRE dialect at regex101.com.
The regex:
^(?:[A-Z]|[A-Z][A-Z]|[A-X][A-F][A-D])(?:[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:
Columns part:
A-Z
, orA-Z
withA-Z
, orA-X
withA-F
withA-D
Rows part:
1-9
, or1-9
with0-9
, or1-9
with1-9
with0-9
etc all the way to the max of104857
with0-6
It matches the following:
A1
AA11
AAA111
ZZ12
YY1048575
XFD1048576
It will not match the following:
A0
AA01
AAZ1111111
XFD1048577
XFE1048576
ZZZ333
ZZZ9999999
Here's the diagram:
这篇关于用于验证Excel单元格的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!