c00p3r.web
c00p3r.web

Reputation: 848

Python: help to make a regexp

I'm not very experienced in regexs.

And now I think I need it for this purpose:

I want the regex to find variables like 5c or c5 (only one variant not both) in a string. In the future the table may grow and i'll need to find vars like 25ac or ac25

The variable would always be number+letter(or two), it's kind of table cells numeration (like in Excel)

/[0-9]+[a-z]{1,2}/

something like that? Can anyone help me with that?

Upvotes: 2

Views: 90

Answers (2)

Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 3838

Here is the simplest solution ...

(\d+\w+|\w+\d+)

Upvotes: 1

ydaetskcoR
ydaetskcoR

Reputation: 56997

You'll want something like this:

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

To break this down, the ^ is the start of a string/line while the $ ends the string/line. You then have a capturing group (what the regex captures in your match) which has a digit character, \d (so 0-9), 1 or more times (which is given by a +) followed by 1 to 2 (signified by the {1,2}) letters (the [] characters contain ranges of characters to match, which in our case is all of the alphabet, uppercase and lowercase) and then an or sign | and then the same pattern but reversed.

http://www.regex101.com/ is a pretty useful resource for playing with regexes and http://www.regular-expressions.info/tutorial.html is a great site showing you everything about regexes.

Upvotes: 2

Related Questions