Reputation: 75
I want a regex that matches "cell" values from A-J rows to 1-10 columns.
For example it should match A10
, A1
, E9
It should not match A100
, A30
, P7
, A01
By the time being, I came up with this regex:
(?:[ABCDEFGHIJabcdefghij][123456789](?![123456789]))(?<=1)(0)?
The only case where it fails is when you give it a A100
cell, it matches the first two characters when in reality it should not return a match.
EDIT: Playing around a little bit, I wrote: (?<!\S)[ABCDEFGHIJabcdefghij]123456789((?<=1)(0))?(?!\S)
Which seems to work for even most cases. I´m still open to suggestions on how to improve it / write it more elegantly.
Upvotes: 1
Views: 39
Reputation: 163517
You can shorten the pattern using a ranges. Then you could match either 10 or 1-9 using an alternation instead of using (?<=1)(0)?
to match 10.
To prevent the partial match, you can use word boundaries.
\b[A-Ja-j](?:10|[1-9])\b
\b
A word boundary[A-Ja-j]
Match either chars in a range from A-J or a-j(?:10|[1-9])
Match either 10 or a single digit 1-9\b
A word boundaryWith whitespace boundaries on the left and right:
(?<!\S)[A-Ja-j](?:10|[1-9])(?!\S)
Upvotes: 2