0

Trying to answer myself an academic exercise here.

Is there a method using Regular Expressions (.net syntax, so see the caveat below) that I can convert fully qualified server name to a combo upper and lower case string (server name is UPPER case, domain name(s) in lower case).

e.g.

  • db01.local => DB01.local
  • DB02.TEST.LOCAL => DB02.test.local
  • db03.LOCAL = > DB03.local

I've been playing around with the RE and so far have ([A-Za-z0-9-]+)\.(.+) as the pattern, but I'm struggling how to do this in a simple one liner.

My initial tests had me fritzing with Matches and getting a returned list, but that feels fugly to me because I then need to check the number of matches, do casting and ToUpper() \ ToLower() operations etc. and, yeah, well...

Caveat: If I wasn't using .NET then I think I should be able to do something simple like use \U${1}.\L${2} as my replacement string, but it doesn't look like .NET supports that syntax.

Rachel Ambler
  • 1,440
  • 12
  • 23
  • So your main problem is finding out [how to use a callback](https://stackoverflow.com/questions/2587866/how-does-matchevaluator-in-regex-replace-work)? – revo Jan 11 '19 at 14:06
  • I'll be honest and say "dunno". As I say, using non .Net regex syntax it seems like I should be able to simply use the substitution pattern I gave in my question and then I'm done. I'm at a total loss on how to do it in .Net. – Rachel Ambler Jan 11 '19 at 14:09

1 Answers1

0

Using the 'Possible duplicate of' link this is what I ended up with:

Regex re = new Regex(@"([A-Za-z0-9-]+)\.(.+)");
foreach (var i in _knownServers)
   clean.Add(re.Replace(i, m => $"{m.Groups[1].ToString().ToUpper()}.{m.Groups[2].ToString().ToLower()}"));
Rachel Ambler
  • 1,440
  • 12
  • 23