Home > Back-end >  Capitalize and add period sign in intials (using Regex Replace)
Capitalize and add period sign in intials (using Regex Replace)

Time:01-10

I want to add period signs to a initials, and ensure that all letters are capitalized. However, if period signs are already given, than don't add one.

Wanted results:
abc => A.B.C.
Abc => A.B.C.
A.b.c => A.B.C.
Ab.C => A.B.C.
ABC => A.B.C.
A.B.C => A.B.C.
A.B.C.=> A.B.C.

I imagined regex replace would be suitable/optimal for this. So far I could only come up with the following code:

Regex.Replace(initials, @"\w{1}", m => m  ".").ToUpper();

This works only in the scenario's when there are no periods in the string, otherwise, it will just add two periods in a row (i.e. a.b.c => a..b..c..)

If other methods than regex replace are more recommended, I warmly welcome them. I work in c#, but wanted to keep the question general to other languages as well.

CodePudding user response:

I suggest

\p{L}\.*

pattern, or (c# code):

initials = Regex.Replace(
  initials, 
 @"\p{L}\.*", 
  m => $"{m.Value.Trim('.').ToUpper()}.");

pattern explained:

\p{L}  - unicode letter
\.*    - zero or more dots ('.')

Demo:

  string[] tests = new string[] {
    "abc",
    "Abc",
    "A.b.c",
    "Ab.C",
    "ABC",
    "A.B.C",
    "A.B.C.",
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,7} => {Regex.Replace(test, @"\p{L}\.*", m => $"{m.Value.Trim('.').ToUpper()}.")}"));

  Console.Write(report);

Outcome:

    abc => A.B.C.
    Abc => A.B.C.
  A.b.c => A.B.C.
   Ab.C => A.B.C.
    ABC => A.B.C.
  A.B.C => A.B.C.
 A.B.C. => A.B.C.

CodePudding user response:

I don't know much about c# but pieced together the following:

using System;
using System.Collections;
using System.Text.RegularExpressions;

class Program
{
    static void Main() {
        var txt = "Ab.C";
        var new_txt = Regex.Replace(txt, @"(\w)\.?", @"$1.").ToUpper();
        Console.Write(new_txt);
    }
}

The pattern (\w)\.? means:

  • (\w) - A capture group of a word-char.
  • \.? - Followed by an optional dot.

Mind you, \w could capture more in c# then just [A-Za-z0-9_]. See the docs here. Meaning you may want to be more specific using a character class.

CodePudding user response:

Approach without RegEx

string result = string.Concat(input.Where(IsLetter).Select(x => char.ToUpper(x)   "."));

https://dotnetfiddle.net/macBWv

CodePudding user response:

You could also make use of the capture collection using a repeated capture group to repeat n times a character followed by an optional dot.

(?:(\w)\.?){3,}

See a .NET regex demo.

For example:

string[] strings = {
    "abc",
    "Abc",
    "Ab.C",
    "ABC",
    "A.B.C",
    "A.B.C."
    };

foreach (String s in strings)
{
    Console.WriteLine(Regex.Replace(
        s,
        @"(?:(\w)\.?){3,}", 
        m => string.Join("", m.Groups[1].Captures.Select(x => x   ".")).ToUpper())
    );
}    

Output

A.B.C.
A.B.C.
A.B.C.
A.B.C.
A.B.C.
A.B.C.
  •  Tags:  
  • Related