Reputation: 14996
I use VS 2012 and RegExp in C#.
I have an regular expression like this:
^CREATE OR REPLACE PACKAGE ([\w]+).([\w]+) IS
I read contents
from a file.
I have troubles with character -
Test_A_B
is OK.
Test-A_B
is KO.
I tried using Regex.Escape
but it's still wrong.
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?
Upvotes: 0
Views: 64
Reputation: 51330
\w
includes alphanumeric characters as well as the underscore. It doesn't include the dash.
Simply replace [\w]+
in your regex with [-\w]+
to include the dash.
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.
Upvotes: 3