简体   繁体   English

在C#中使用字符“-”的正则表达式

[英]Regular Expression using character “-” in C#

I use VS 2012 and RegExp in C#. 我在C#中使用VS 2012和RegExp。
I have an regular expression like this: 我有一个这样的正则表达式:

^CREATE OR REPLACE PACKAGE ([\w]+).([\w]+) IS

I read contents from a file. 我从文件中读取contents

I have troubles with character - 我性格有问题-

Test_A_B is OK. Test_A_B可以。
Test-A_B is KO . Test-A_BKO

I tried using Regex.Escape but it's still wrong. 我尝试使用Regex.Escape但这仍然是错误的。

Unit test code: 单元测试代码:

var pattern1 = @"^CREATE OR REPLACE PACKAGE ([\w]+).([\w]+) IS";
var regexPackage = new Regex(pattern1);

var contents = @"CREATE OR REPLACE PACKAGE ZZZ.Test_A_B IS";
match1 = regexPackage.Match(contents);
Assert.IsTrue(match1.Success);
Assert.AreNotEqual(0, match1.Groups.Count);
Assert.AreEqual("Test_A_B", match1.Groups[2].Value);

contents = @"CREATE OR REPLACE PACKAGE ZZZ.Test-A_B IS";
//contents = Regex.Escape(contents);
match1 = regexPackage.Match(contents);
Assert.IsTrue(match1.Success); <===== FAILS
Assert.AreNotEqual(0, match1.Groups.Count);
Assert.AreEqual("Test-A_B", match1.Groups[2].Value);

Any suggestions? 有什么建议么?

\\w includes alphanumeric characters as well as the underscore. \\w包括字母数字字符和下划线。 It doesn't include the dash. 它不包括破折号。

Simply replace [\\w]+ in your regex with [-\\w]+ to include the dash. 只需将正则表达式中的[\\w]+替换为[-\\w]+即可包含破折号。

As a side note, the dash has a special meaning in character classes. 作为附带说明,破折号在字符类中具有特殊含义。 It either needs to be first in the class, or last, or be escaped with a backslash. 它要么必须是该类中的第一个,要么是最后一个,或者以反斜杠进行转义。

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

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