0

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

AviFarah
  • 327
  • 1
  • 10
  • Please add a sample of the data you are trying to match against, and the associated desired matches. – Drunken Code Monkey May 17 '19 at 02:10
  • @DrunkenCodeMonkey, I did n apologize for not making it clear enough. See variables: textDashInMiddle, textDashAtEnd and textDashInTheBeginning. The desired match is the whole text. – AviFarah May 17 '19 at 02:41
  • 1
    It looks like `\b` is treating the leading/trailing dashes as boundaries. – madreflection May 17 '19 at 02:43
  • @madreflection, it could be, I will take the \b out though in vs2017, work code, I did not have boundaries. – AviFarah May 17 '19 at 02:47

2 Answers2

0

From what I can gather, something like this pattern should work:

[a-zA-Z0-9\-]{5}

You just define a character class containing alpha numerical characters and the dash, and you look for more than one character grouped together (5 in this case)... This will return -1234, 123-4, 1234-, etc..

Drunken Code Monkey
  • 1,796
  • 1
  • 14
  • 18
  • thank you. The real problem is my @"\b" leading and trailing character in the pattern matching which will match but not capture on a "-" character. – AviFarah Jun 24 '19 at 02:01
0

@madreflection Was correct the moment I changed the boundary characters (\b) to (^) n ($) my RE worked. I will check my work expression when I get there.

Thank you all

AviFarah
  • 327
  • 1
  • 10