How to get the word before the last word from a string (edge‑case‑safe) in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        Console.WriteLine("=== Testing: Get Word Before Last ===\n");

        string[] testCases =
        {
            "python c#",
            "  many   spaces   here   now  ",
            "OneWord",
            "",
            "   ",
            "Hello, world!",
            "Tabs\tand\nnewlines work too",
            "Unicode 世界、こんにちは",
            "Ends with punctuation.",
            "Multiple words, with punctuation, here!",
            "state-of-the-art program example"
        };

        foreach (var test in testCases)
        {
            string result = GetWordBeforeLast(test);

            Console.WriteLine($"Input: \"{test}\"");
            Console.WriteLine("Output: " + (result ?? "null"));
            Console.WriteLine(new string('-', 40));
        }
    }

    static string GetWordBeforeLast(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
            return null;

        // Extract words (letters, digits, underscore)
        // \p{L}+ handles Unicode letters
        var matches = Regex.Matches(text, @"\p{L}+");

        if (matches.Count < 2)
            return null;

        return matches[matches.Count - 2].Value;
    }
}



/*
OUTPUT:

=== Testing: Get Word Before Last ===

Input: "python c#"
Output: python
----------------------------------------
Input: "  many   spaces   here   now  "
Output: here
----------------------------------------
Input: "OneWord"
Output: null
----------------------------------------
Input: ""
Output: null
----------------------------------------
Input: "   "
Output: null
----------------------------------------
Input: "Hello, world!"
Output: Hello
----------------------------------------
Input: "Tabs	and
newlines work too"
Output: work
----------------------------------------
Input: "Unicode 世界、こんにちは"
Output: 世界
----------------------------------------
Input: "Ends with punctuation."
Output: with
----------------------------------------
Input: "Multiple words, with punctuation, here!"
Output: punctuation
----------------------------------------
Input: "state-of-the-art program example"
Output: program
----------------------------------------

*/

 



answered Mar 28 by avibootz
edited Mar 28 by avibootz

Related questions

...