none
Display specific words from end of user input RRS feed

  • Question

  • Hello,

    I've been looking around on the internet to see if it would be possible to print the last three words or so of a userinput to the screen. I've not had much luck so hope someone may be able to help with this? Would be greatly appreciated.

    Tuesday, December 18, 2018 9:40 AM

Answers

  • If the words are separated by spaces only, then check this example:

    string text = "qwe rtyui op asd fghjkl";

    string result = Regex.Match( text, @"\w+\s+\w+\s+\w+(?=\s*$)" ).Value;

    Console.WriteLine( text );

    Console.WriteLine( result ); // "op asd fghjkl"


    • Marked as answer by 32garfield Tuesday, December 18, 2018 11:37 AM
    Tuesday, December 18, 2018 11:30 AM

All replies

  • If the words are separated by spaces only, then check this example:

    string text = "qwe rtyui op asd fghjkl";

    string result = Regex.Match( text, @"\w+\s+\w+\s+\w+(?=\s*$)" ).Value;

    Console.WriteLine( text );

    Console.WriteLine( result ); // "op asd fghjkl"


    • Marked as answer by 32garfield Tuesday, December 18, 2018 11:37 AM
    Tuesday, December 18, 2018 11:30 AM
  • If the words are separated by spaces only, then check this example:

    string text = "qwe rtyui op asd fghjkl";

    string result = Regex.Match( text, @"\w+\s+\w+\s+\w+(?=\s*$)" ).Value;

    Console.WriteLine( text );

    Console.WriteLine( result ); // "op asd fghjkl"


    It worked perfectly! Thank you very much!
    Tuesday, December 18, 2018 11:41 AM