简体   繁体   English

无法在 static 上下文中访问非静态方法“IsMatch”

[英]Cannot access non-static method 'IsMatch' in static context

I am not able to figure out the simple compile error I am getting on Regex.Match method.我无法弄清楚我在Regex.Match方法上遇到的简单编译错误。

The error says错误说

Cannot access non-static method 'IsMatch' in static context无法在 static 上下文中访问非静态方法“IsMatch”

在此处输入图像描述

The error text is confusing in itself because the method I am calling is the static method of the Regex class while the error says that I cannot access non-static method in statc context.错误文本本身令人困惑,因为我调用的方法是正则Regex class 的 static 方法,而错误表明我无法在 statc 上下文中访问non-static方法。

My context is also not static because the method in which I have calling Regex.IsMatch function is a non-static method, even the class is also non static. My context is also not static because the method in which I have calling Regex.IsMatch function is a non-static method, even the class is also non static.

The error is confusing!错误令人困惑!

It works when I replace the call with the Regex class' instance call like this当我像这样用 Regex 类的实例调用替换调用时,它可以工作

new Regex(@"/^(?:2[89]|[3-9]\d)\d{6,}$/").IsMatch(@"/^(?:2[89]|[3-9]\d)\d{6,}$/")

But I am totally confused of why can't I call the static method of the class!但是我完全困惑为什么我不能调用该类的 static 方法!

There is no static method IsMatch accepting only one parameter.没有static方法IsMatch只接受一个参数。

IsMatch(string input, string pattern) is static , but IsMatch(string input) is not and needs to be called on an instance that already has been given a pattern (because, thinking about it, what pattern would be used if this method was static and we only provide an input string to it?) IsMatch(string input, string pattern)static ,但是IsMatch(string input)不是,需要在已经被赋予模式的实例上调用(因为,考虑一下,如果这种方法是static我们只提供一个输入字符串给它?)

Here is where you can find all available overloads. 在这里您可以找到所有可用的重载。

You need to provide an input string to the Regex.IsMatch method .您需要向Regex.IsMatch方法提供输入字符串。

However, you also need to amend the regex and remove the leading and trailing slashes that you must be using as regex delimiter chars.但是,您还需要修改正则表达式并删除必须用作正则表达式分隔符字符的前导和尾随斜杠。 Those slashes are treated as part of a pattern and will prevent your regex from matching any string.这些斜杠被视为模式的一部分,将阻止您的正则表达式匹配任何字符串。 Remember that regular expressions are defined with string literals ( "..." ) in C#, not with regex literals ( /.../ ).请记住,正则表达式是用 C# 中的字符串文字 ( "..." ) 定义的,而不是用正则表达式文字 ( /.../ ) 定义的。

So you need to use所以你需要使用

Regex.IsMatch(item.HSCode, @"^(?:2[89]|[3-9]\d)\d{6,}$")

Also, consider using [0-9] instead of \d if you only expect ASCII digits in the input, see \d less efficient than [0-9] .此外,如果您只希望输入中的 ASCII 数字,请考虑使用[0-9]而不是\d ,请参阅\d 比 [0-9] 效率低

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

相关问题 在非静态上下文中无法访问静态方法 - Cannot access static method in a non-static context 无法在静态上下文中访问非静态方法? - Cannot access non-static method in static context? 无法在静态上下文中访问非静态字段“lblSystemStatus” - Cannot access non-static field 'lblSystemStatus' in static context 在静态上下文中无法访问非静态字段 - Cannot access non-static field in static context 嵌套类:无法访问静态上下文中的非静态字段 - Nested class: Cannot access non-static field in static context C#编译器:无法在非静态上下文中访问静态方法 - C# Compiler : cannot access static method in a non-static context C#无法在静态上下文中访问非静态成员字段,除非实际处于静态上下文中 - C# Cannot access non-static member field in static context, without actually being in static context 无法通过APM扩展访问TAP中的非静态方法 - Cannot access non-static method in TAP over APM extension 使用依赖属性解决“无法访问 static 上下文中的非静态属性”的另一种方法 - An alternative way around 'Cannot access non-static property in static context' using dependency property 为什么我在使用动态时能够在静态上下文中访问非静态方法 - Why am I able to access a non-static method in a static context when using dynamic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM