Regex help

G

Guest

Hi Folks,

Can anyone assis me in creating a regex for the following formats

AA.
AA.A
AAA
AAAA

A denotes alpha characters only

Thanks in advance
 
K

Kevin Spencer

What are the rules that define the pattern?

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
J

Jesse Houwing

Hello Shem,
Can anyone assis me in creating a regex for the following formats

AA.
AA.A
AAA
AAAA
A denotes alpha characters only

These are all very simple formats. You should be able to figure it ot quite
easily. Regardless here's how I'd do it:

^(?i:[a-z]{2}(?:\.[a-z]?|[a-z]{1,2}))$

I'll explain:

^ beginning of teh string
(?i:....) case insensitive matching within the parenthesis
[a-z]{2} first two alphanumeric characters (all formats begin with two characters)
(?:...) non-capturing group, is faster than just (...)
\.[a-z]? escape the . as it's a special character in regex followed by an
optional character (first two formats are now satisfied)
| OR....
[a-z]{1,2} one or two more characters (the thrid and fourth are now also
satisfied)
$ end of the input.

Kind regards,

Jess
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top