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

编程入门 行业动态 更新时间:2024-10-25 04:20:56
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}\*?

更多推荐

本文发布于:2023-08-07 21:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   regex   match   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!