How to get the 1st char from each word with the whole number in a sentence?

I have sentencesthose designs with wordsand digits. I want to get stringthat comprise 1st charof all word, all digitand the word has a all upper casecapital letter. I tried using Regex, but the problem is that it does not give letters all digitand all upper case.

My Regex is located in Regex101 .

My solution is in DotNetFiddle .

CODE:

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

public class Program
{
    public static void Main()
    {
        List<string> list = new List<string> {"Freestyle steel","Freestyle Alloy","Trekking steel uk","Single speed","5 speed","15 speed","3 Speed internal gear with 55 coaster","MTB steel","Junior MTB"};
        foreach(string data in list)
        {
            string regex = @"(\b\w)|(\d+)";
            var matches = Regex.Matches(data, regex, RegexOptions.Multiline);
            string output = "";
            foreach(Match item in matches)
            {
                output += item.Groups[1];
            }
            Console.WriteLine(output);
        }
    }
}

Input example

Free steel

Freestyle Alloy

Marching steel uk

Single speed

5 speeds

15 speed

3 Speed ​​Internal Gear with 55 Coaster

Steel MTB

Junior MTB

Sample output

Fs

F

Tsa

Ss

5s

15s

3Sigw55c

torpedo boats

Jmtb

+4
3

, ,

@"[0-9]+|\b(?:\p{Lu}+\b|\w)"

  • [0-9]+ -
  • | -
  • \b -
  • (?:\p{Lu}+\b|\w) - 1 + , (\p{Lu}+\b) char (\w).

. :

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        var regex = @"[0-9]+|\b(?:\p{Lu}+\b|\w)";
        var list = new List<string> {"Freestyle steel","Freestyle Alloy","Trekking steel uk","Single speed","5 speed","15 speed","3 Speed internal gear with 55 coaster","MTB steel","Junior MTB"};
        foreach(var data in list)
        {
            var matches = Regex.Matches(data, regex).Cast<Match>().Select(m => m.Value.ToUpper());
            Console.WriteLine(string.Join("", matches));
        }
    }
}

:

FS
FA
TSU
SS
5S
15S
3SIGW55C
MTBS
JMTB
+1

\d+|\b(?:[A-Z]+|\w)

regex101.com.

+1

:

string input = "3 Speed internal gear with 55 coaster";
string pattern = @"\B[a-z]+|\W+";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

\B (--) , , [a-z], , \W .

+1

Source: https://habr.com/ru/post/1675415/


All Articles