tags:

views:

179

answers:

4

Hi, I know there are plenty of questions on SO asking for regex help so I apologise in advance for yet another.

I've never used regular expressions before and I've searched online (and downloaded a program to show you the results of your regex) but I can't seem to figure the darn thing out myself which is annoying because I know it's really easy.

I have lots of lines of text taken from a csv file. Most lines are of the format:

Serial Number, Description, Status

I need to know which lines contain serial numbers. The serial numbers are generally of the format ABC001. But sometimes there's 4 letters, sometimes 4 numbers etc. So I tried to make an expression that just checked the first digit is a letter and the last digit before the first comma is a number. I know it's not perfect but it's completely fine for my purposes.

I tried ^[A-Z]$[0-9] as I thought 'starts with A-Z, ends with 0-9' but this isn't working. Could someone please help me as it's driving me mad!

I don't know this makes a difference but I'm using C#.

+1  A: 

my proposition:

^[A-Za-z]{3,4}[0-9]{3,4}
dfens
The OP didn't mention whether the letters are case-sensitive or not. I'm assuming not, in which case this is the right answer, but if it is, just change the character set in the first group. (Note: I don't think you want the `$` at the end, because there's more that comes after the serial number.)
John Feminella
@John Feminella, thx mate
dfens
They're not case sensitive, but cheers for including that!
RichK
A: 

Starts with a letter last digit before the first comma is a number:

^[a-zA-Z][^,]*[0-9],
Antony Hatchkins
A: 

If you're not familiar with regex, why not just use normal string operations with C#? I mean if someone provides a solution (which probably will happen), you'll very likely not be able to confirm if it works for all your cases, or will not be able to adjust it if the need to do so arises.

Bart Kiers
A: 

If you want "starts with A-Z, ends with 0-9" you can just do [A-Z][A-Z0-9]+[0-9]. Your previous regex doesn't work because ^ and $ look for the beginnings and ends of lines.

eliah
Cheers eliah, I thought they defined the start and end of the actual expression, oops.
RichK