开发者

Regular Expression for determining serial number

开发者 https://www.devze.com 2022-12-18 02:35 出处:网络
I know there are plenty of questions on SO asking for regex help so I apologise in advance for yet another.

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开发者_C百科 makes a difference but I'm using C#.


my proposition:

^[A-Za-z]{3,4}[0-9]{3,4}


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.


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.


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

^[a-zA-Z][^,]*[0-9],
0

精彩评论

暂无评论...
验证码 换一张
取 消