简体   繁体   English

在c#上表示通配符

[英]representing wildcards on c#

I'm trying to implement the two oracle wildcards "%" and "_ " on my c# program using the Regex class. 我正在尝试使用Regex类在我的c#程序上实现两个oracle通配符“%”和“_”。 The problem is when I have the wildcard "_" because I need to accept ONLY ONE character but it returns true everytime that I have AT LEAST ONE character. 问题是当我有通配符“_”因为我只需要接受一个字符,但每次我至少有一个字符时它返回true。 Can you please help me? 你能帮我么?

Here's my code: 这是我的代码:

string filter, string1;

string wildcard1 = "[" + "\\d | " + "\\n | " + "\\s |" + "\\w]+";
string wildcard2 = "[" + "\\d | " + "\\n |" + "\\s | " + "\\w]{1}";

filter = Regex.Replace(filter, "%", wildcard1);
filter = Regex.Replace(filter, "_", wildcard2);

Regex regex1 = new Regex(filter, RegexOptions.IgnoreCase);

MatchCollection a = regex1.Matches(string1);

if (regex1.IsMatch(string1))
{
    return true;
}

You have to make these two conversions: 您必须进行以下两次转换:

  • % to .* , which means, "any character, any number of times" % to .* ,表示“任何字符,任意次”
  • _ to . _. which means "any character (one time by default if you don't specify otherwise)" 这意味着“任何字符(如果您没有另外指定,则默认为一次)”

You also have to add ^ at the start and $ at the end, to force to match the whole string, and not a part inside it. 你还必须在开头添加^ ,在结尾添加$ ,强制匹配整个字符串,而不是其中的一部分。

And finally, and this is very important, you have to escape the original string . 最后,这非常重要,你必须逃避原来的字符串 If you don't do it, any special character will be processed by the regex. 如果不这样做,正则表达式将处理任何特殊字符。 Ie a dot . 就是一个点. in original string will be interpreted "as any character" in the regular expression. 在原始字符串中将在正则表达式中解释为“任何字符”。

For example, if you have this SQL LIKE string: 例如,如果您有此SQL LIKE字符串:

  Hello, %, is your inital _?

You have to convert it to: 你必须将其转换为:

  ^Hello, .*, is your inital .\?$

(This will match strings like "Hello, John, is your initial J?") (这将匹配像“你好,约翰,你的初始J?”这样的字符串)

First, you have to escape it, so that the ? 首先,你必须逃避它,以便? and any other special character is escaped with backslash \\ . 并使用反斜杠\\来转义任何其他特殊字符。

And then replace % and _ and add the start and end of string characters ( ^ and $ ). 然后替换%_并添加字符串字符的开头和结尾( ^$ )。

Be also aware that you can create a regex with options, and one of this options let you specify if it's case sensitive or not. 还要注意,您可以使用选项创建正则表达式,其中一个选项可让您指定它是否区分大小写。 This will also be neccesary to mimic Oracle behaviour. 这也是模仿Oracle行为的必要条件。

Finally, use IsMatch(string) method instead of Match. 最后,使用IsMatch(string)方法而不是Match。

With such regex you will get a match if any substring matches. 使用这样的正则表达式,如果任何子字符串匹配,您将获得匹配。 Thus if you ask for abc_ and the input string is abcde , it will match too, because the substring abcd matches. 因此,如果你要求abc_并且输入字符串是abcde ,它也将匹配,因为子字符串abcd匹配。 Wrap your filter with ^ and $ to avoid this. ^$包装你的过滤器以避免这种情况。

Besides that, making initial replacement, when building your Regex, like here: 除此之外,在构建正则表达式时进行初始替换,如下所示:

filter = Regex.Replace(filter, "%", wildcard1);
filter = Regex.Replace(filter, "_", wildcard2);

is asking for trouble, unless you make sure that characters you replace are not regex special characters. 除非你确定你替换的字符不是正则表达式特殊字符,否则要求麻烦。 Just use normal string replacement for this. 只需使用普通的字符串替换。

Plus, you can just use regex.IsMatch instead of Matches . 另外,您可以使用regex.IsMatch而不是Matches

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

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