Home > Back-end >  Need help using regex function/trimming my string
Need help using regex function/trimming my string

Time:11-26

I have been stuck on this for a bit now, my output looks as follows: the first 2 letters represents the hallway number, so 17 for the first one, and the following number represents the shelf number, (location in hallway). As you can see in hallway 17 shelf 1 we have either A1 or A, but that doesn't matter. I want the output for 171A1 to be 171, and for 15211 to be 1521, so I want to remove the alphabetic letters at the end combined with the numbers which may follow after.

171A1
171A1
171A
171A0
15211
15211
15211
15210
15190

I tried using string.Remove(string.Length-2) but this doesn't work as we have 171A for example, which should become 171. Any help would be appreciated.

CodePudding user response:

In case you don't need it to be REGEX, this should work

var rawString = "15211";
var maxLength = 4;
var trimmedResult = new string(
        rawString.TakeWhile(char.IsNumber) // loop through characters using LINQ. since your string starts with numbers, you can just keep taking until you bump into a character
            .Take(maxLength) // limit the length for cases like "15211" where you don't need the extra numbers in the end
            .ToArray() // so we can use the new string() constructor
        );

CodePudding user response:

For the part in the question, you can use a pattern with a capture group, and use the group in the replacement:

^([0-9]{2,}?)[A-Z]?[0-9]?$
  • ^ Start of string
  • ([0-9]{2,}?) Capture group 1, match 2 digits 0-9 non greedy
  • [A-Z]?[0-9]? Match an optional char A-Z and optional digit 0-9
  • $ End of string

See a regex demo.

string pattern = @"^([0-9]{2,}?)[A-Z]?[0-9]?$";
string[] strings = { "171A1", "171A1", "171A", "171A0", "15211", "15211", "15211", "15210", "15190" };
foreach (String s in strings)
{
    Console.WriteLine(Regex.Replace(s, pattern, "$1"));
}

Output

171
171
171
171
1521
1521
1521
1521
1519

If you want to separate the output by a dot after the 2 digits, you can use 2 capture groups and match at least 1 or more digits in the second group:

string pattern = @"^([0-9]{2})([0-9] ?)[A-Z]?[0-9]?$";
string[] strings = { "171A1", "171A1", "171A", "171A0", "15211", "15211", "15211", "15210", "15190" };
foreach (String s in strings)
{
    Console.WriteLine(Regex.Replace(s, pattern, "$1.$2"));
}

Output

17.1
17.1
17.1
17.1
15.21
15.21
15.21
15.21
15.19

See a C# demo

  • Related