Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to get everything after the first space

Tags:

string

c#

regex

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.

like image 542
torres Avatar asked Apr 13 '13 12:04

torres


2 Answers

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

\s(.*)
like image 66
torres Avatar answered Sep 20 '22 07:09

torres


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(' '));
like image 36
MarcinJuraszek Avatar answered Sep 19 '22 07:09

MarcinJuraszek