19

What would the syntax to get all the words in a string after the first space. For example, bobs nice house. So the result should be " nice house" without the quote.

([^\s]+) gives me all 3 words seperated by ;

,[\s\S]*$ > not compiling.

torres
  • 1,283
  • 8
  • 21
  • 30

4 Answers4

29

I was really looking shortest possible code. following did the job. thanks guys

\s(.*)
torres
  • 1,283
  • 8
  • 21
  • 30
7

I think it should be done this way:

[^ ]* (.*)

It allows 0 or more elements that are not a space, than a single space and selects whatever comes after that space.

C# usage

var input = "bobs nice house";

var afterSpace = Regex.Match(input, "[^ ]* (.*)").Groups[1].Value;

afterSpace is "nice house".

To get that first space as well in result string change expression to [^ ]*( .*)

No regex solution

var afterSpace = input.Substring(input.IndexOf(' '));
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
2

Actually, you don't need to use regex for that process. You just need to use String.Split() method like this;

string s = "bobs nice house";
string[] s1 = s.Split(' ');
for(int i = 1; i < s1.Length; i++)
    Console.WriteLine(s1[i]);

Output will be;

nice
house

Here is a DEMO.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

use /(?<=\s).*/g to find string after first space.run snippet check

str="first second third forth fifth";
str=str.match(/(?<=\s).*/g)
console.log(str);
Syed Haseeb
  • 212
  • 2
  • 9