为什么点符号在这个正则表达式中不起作用?

时间:2022-05-02 21:46:02

NET Regex. (with ignore-case)

NET正则表达式。 (使用ignore-case)

I want it to match

我希望它匹配

field_x_12
field_a_ABC
field_r_something

etc

等等

My question is why the . operator doesn't work in this regex:

我的问题是为什么。运算符在此正则表达式中不起作用:

field_[.]_.*

yet this (equivalent basically) regex does work:

然而这个(相当于基本的)正则表达式确实有效:

field_[a-z]_.*

Is there something I'm missing about the dot operator . ?

关于点运算符有什么我想念的吗? ?

5 个解决方案

#1


6  

A . inside a character class ([...]) is a literal dot character. If you want it to act as a wildcard, don't use the brackets.

一个 。在一个字符类([...])里面是一个文字点字符。如果您希望它充当通配符,请不要使用括号。

#2


1  

Why are you using [.]? The [] denotes a explicit set of characters, so the . character is what the RegEx is looking for.

你为什么用[。]? []表示一组明确的字符,所以。字符是RegEx正在寻找的。

field_._.*

Should work fine.

应该工作正常。

See this handy .NET RegEx cheat sheet.

请参阅这个方便的.NET RegEx备忘单。

#3


0  

Inside brackets . is a literal dot and doesn't match any character.

括号内。是一个文字点,不匹配任何字符。

#4


0  

You should try field_._.* because inside the [] it is treatd as an acutal dot.

您应该尝试字段_._。*因为在[]内部它被视为一个实际点。

#5


0  

When it's inside a character class, the dot is just a period, not a wildcard.

当它在一个字符类中时,点只是一个句点,而不是通配符。

#1


6  

A . inside a character class ([...]) is a literal dot character. If you want it to act as a wildcard, don't use the brackets.

一个 。在一个字符类([...])里面是一个文字点字符。如果您希望它充当通配符,请不要使用括号。

#2


1  

Why are you using [.]? The [] denotes a explicit set of characters, so the . character is what the RegEx is looking for.

你为什么用[。]? []表示一组明确的字符,所以。字符是RegEx正在寻找的。

field_._.*

Should work fine.

应该工作正常。

See this handy .NET RegEx cheat sheet.

请参阅这个方便的.NET RegEx备忘单。

#3


0  

Inside brackets . is a literal dot and doesn't match any character.

括号内。是一个文字点,不匹配任何字符。

#4


0  

You should try field_._.* because inside the [] it is treatd as an acutal dot.

您应该尝试字段_._。*因为在[]内部它被视为一个实际点。

#5


0  

When it's inside a character class, the dot is just a period, not a wildcard.

当它在一个字符类中时,点只是一个句点,而不是通配符。