C#正则表达式匹配字符串(C# regex to match string)

我目前在使用正则表达式匹配字符串时遇到了困难。 目标是匹配:

一两个字母 一个,两个或三个数字 零或一个星号

如U21,F305和H12 *。 我正在使用的正则表达式是:

\D{1,2}\d{1,3}\*?

但是,它一直在匹配字符串:

3.0L 6HBW20 3/8" Y1015

我对正则表达式并不是太明亮,但是这让我无法完成我的项目。 谁能帮我吗?

谢谢。

I'm currently having difficulty matching strings with my regex. The objective is to match:

One or two letters One, two or three numbers Zero or one asterisk

Such as U21, F305 and H12*. The regex that I'm using is:

\D{1,2}\d{1,3}\*?

However, it's been matching strings like:

3.0L 6HBW20 3/8" Y1015

I'm not too bright with regex, but this is holding me from completing my project. Can anyone help me out?

Thank you.

最满意答案

\D匹配任何非数字,这是一个比字母大得多的集合(基本上所有其他东西 ,包括句号,斜杠等)。 尝试使用[a-zA-Z]{1,2}匹配1或2个字母。

[a-zA-Z]{1,2}\d{1,3}\*?

\D matches any non-digit, which is a much larger set than just letters (basically everything else, including periods, slashes, etc). Try using [a-zA-Z]{1,2} to match 1 or 2 letters.

[a-zA-Z]{1,2}\d{1,3}\*?C#正则表达式匹配字符串(C# regex to match string)

我目前在使用正则表达式匹配字符串时遇到了困难。 目标是匹配:

一两个字母 一个,两个或三个数字 零或一个星号

如U21,F305和H12 *。 我正在使用的正则表达式是:

\D{1,2}\d{1,3}\*?

但是,它一直在匹配字符串:

3.0L 6HBW20 3/8" Y1015

我对正则表达式并不是太明亮,但是这让我无法完成我的项目。 谁能帮我吗?

谢谢。

I'm currently having difficulty matching strings with my regex. The objective is to match:

One or two letters One, two or three numbers Zero or one asterisk

Such as U21, F305 and H12*. The regex that I'm using is:

\D{1,2}\d{1,3}\*?

However, it's been matching strings like:

3.0L 6HBW20 3/8" Y1015

I'm not too bright with regex, but this is holding me from completing my project. Can anyone help me out?

Thank you.

最满意答案

\D匹配任何非数字,这是一个比字母大得多的集合(基本上所有其他东西 ,包括句号,斜杠等)。 尝试使用[a-zA-Z]{1,2}匹配1或2个字母。

[a-zA-Z]{1,2}\d{1,3}\*?

\D matches any non-digit, which is a much larger set than just letters (basically everything else, including periods, slashes, etc). Try using [a-zA-Z]{1,2} to match 1 or 2 letters.

[a-zA-Z]{1,2}\d{1,3}\*?