简体   繁体   English

用于确定序列号的正则表达式

[英]Regular Expression for determining serial number

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. 我从csv文件中提取了很多行文本。 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. 序列号通常采用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. 但是有时候有4个字母,有时有4个数字,等等。所以我试图做一个表达式,检查第一个数字是一个字母,第一个逗号之前的最后一个数字是一个数字。 I know it's not perfect but it's completely fine for my purposes. 我知道这并不完美,但出于我的目的完全可以。

I tried ^[AZ]$[0-9] as I thought 'starts with AZ, ends with 0-9' but this isn't working. 我尝试了^ [AZ] $ [0-9],因为我认为“以AZ开头,以0-9结尾”,但这是行不通的。 Could someone please help me as it's driving me mad! 有人可以帮助我,因为它使我发疯!

I don't know this makes a difference but I'm using C#. 我不知道这有什么区别,但是我正在使用C#。

我的主张:

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

以字母最后一个数字开头,第一个逗号是数字之前:

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

If you're not familiar with regex, why not just use normal string operations with C#? 如果您对正则表达式不熟悉,为什么不只对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 AZ, ends with 0-9" you can just do [AZ][A-Z0-9]+[0-9] . 如果要“以AZ开头,以0-9结尾”,则可以执行[AZ][A-Z0-9]+[0-9] Your previous regex doesn't work because ^ and $ look for the beginnings and ends of lines. 您以前的正则表达式不起作用,因为^$寻找行的开头和结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM