I would like to match on alphanumeric or dash ("-") characters.
My naive pattern of @"\b(?<alphaOrDash>[\-\w]{5})\b"
seems to match alphanumeric-dash character strings for as long as the dash is not the leading or trailing dash.
Can anyone give me an insight into this conundrum. My expectation is that a leading or trailing dash should make no difference and I should be able to match on a leading and trailing dashes. Where do I go wrong?
Code that exemplifies the above question (I wrote this code in LINQPad and I got the same result in VS2017):
var textDashInMiddle = "123-4";
var patMatch5 = @"\b(?<fiveChars>[\-\w]{5})\b";
var reMatch5 = new Regex(patMatch5);
var match5 = reMatch5.Match(textDashInMiddle);
if (match5.Success)
Console.WriteLine($"1. {match5.Groups["fiveChars"].Value}");
else
Console.WriteLine("1. No success");
var textDashAtEnd = "1234-";
match5 = reMatch5.Match(textDashAtEnd);
if (match5.Success)
Console.WriteLine($"2. {match5.Groups["fiveChars"].Value}");
else
Console.WriteLine("2. No success");
var textDashInTheBeginning = "-1234";
match5 = reMatch5.Match(textDashInTheBeginning);
if (match5.Success)
Console.WriteLine($"3. {match5.Groups["fiveChars"].Value}");
else
Console.WriteLine("3. No success");
var patMatchAll = @"\b(?<fiveChars>[\-\w]+)\b";
//var patMatchAll = @"\b(?<fiveChars>(\-|\w)+)\b";
var reMatchAll = new Regex(patMatchAll);
var matchAll = reMatchAll.Match(textDashInMiddle);
if (matchAll.Success)
Console.WriteLine($"4. {matchAll.Groups["fiveChars"].Value}, {matchAll.Groups["fiveChars"].Value.Length}");
else
Console.WriteLine("4. No success");
matchAll = reMatchAll.Match(textDashAtEnd);
if (matchAll.Success)
Console.WriteLine($"5. {matchAll.Groups["fiveChars"].Value}, {matchAll.Groups["fiveChars"].Value.Length}");
else
Console.WriteLine("5. No success");
matchAll = reMatchAll.Match(textDashInTheBeginning);
if (matchAll.Success)
Console.WriteLine($"6. {matchAll.Groups["fiveChars"].Value}, {matchAll.Groups["fiveChars"].Value.Length}");
else
Console.WriteLine("6. No success");
The result of running the above code is:
1. 123-4
2. No success
3. No success
4. 123-4, 5
5. 1234, 4
6. 1234, 4
Thank you