C# 正则表达式获取第一个空格后的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15987650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to get everything after the first space
提问by torres
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]+)
给我所有由 ; 分隔的 3 个词
,[\s\S]*$ >
not compiling.
,[\s\S]*$ >
不编译。
回答by MarcinJuraszek
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.
它允许 0 个或多个不是空格的元素,而不是单个空格,并选择该空格之后的任何元素。
C# usage
C# 用法
var input = "bobs nice house";
var afterSpace = Regex.Match(input, "[^ ]* (.*)").Groups[1].Value;
afterSpace
is "nice house"
.
afterSpace
是"nice house"
。
To get that first space as well in result string change expression to [^ ]*( .*)
要在结果字符串中也获得第一个空格,请将表达式更改为 [^ ]*( .*)
No regex solution
没有正则表达式解决方案
var afterSpace = input.Substring(input.IndexOf(' '));
回答by Soner G?nül
Actually, you don't need to use regex for that process. You just need to use String.Split()
method like this;
实际上,您不需要在该过程中使用正则表达式。你只需要使用这样的String.Split()
方法;
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
.
这是一个DEMO
.
回答by torres
I was really looking shortest possible code. following did the job. thanks guys
我真的在寻找最短的代码。以下做了这项工作。谢谢你们
\s(.*)