How to crop strings using regular expressions? [closed]
Here's the phrase I'd like to convert.
Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC
I wanna pick up these words Summoner1, Summoner2, Summoner3, Summoner4
.
I suppose I should detect the strings "joined the lobby"
, and ": "
using regular expression(regex) but I have no clue how to.
Thank you in advance.
Codes for extra question.var list = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));
c# regex
closed as too broad by Daniel A. White, Alexei Levenkov, Daniel Mann, ggorlen, gnat Jan 19 at 7:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Here's the phrase I'd like to convert.
Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC
I wanna pick up these words Summoner1, Summoner2, Summoner3, Summoner4
.
I suppose I should detect the strings "joined the lobby"
, and ": "
using regular expression(regex) but I have no clue how to.
Thank you in advance.
Codes for extra question.var list = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));
c# regex
closed as too broad by Daniel A. White, Alexei Levenkov, Daniel Mann, ggorlen, gnat Jan 19 at 7:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
input.Split(new {':',' '}).First()
– TheGeneral
Jan 19 at 3:51
add a comment |
Here's the phrase I'd like to convert.
Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC
I wanna pick up these words Summoner1, Summoner2, Summoner3, Summoner4
.
I suppose I should detect the strings "joined the lobby"
, and ": "
using regular expression(regex) but I have no clue how to.
Thank you in advance.
Codes for extra question.var list = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));
c# regex
Here's the phrase I'd like to convert.
Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC
I wanna pick up these words Summoner1, Summoner2, Summoner3, Summoner4
.
I suppose I should detect the strings "joined the lobby"
, and ": "
using regular expression(regex) but I have no clue how to.
Thank you in advance.
Codes for extra question.var list = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));
c# regex
c# regex
edited Jan 19 at 6:41
XHSKR
asked Jan 19 at 3:34
XHSKRXHSKR
12
12
closed as too broad by Daniel A. White, Alexei Levenkov, Daniel Mann, ggorlen, gnat Jan 19 at 7:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Daniel A. White, Alexei Levenkov, Daniel Mann, ggorlen, gnat Jan 19 at 7:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
input.Split(new {':',' '}).First()
– TheGeneral
Jan 19 at 3:51
add a comment |
input.Split(new {':',' '}).First()
– TheGeneral
Jan 19 at 3:51
input.Split(new {':',' '}).First()
– TheGeneral
Jan 19 at 3:51
input.Split(new {':',' '}).First()
– TheGeneral
Jan 19 at 3:51
add a comment |
2 Answers
2
active
oldest
votes
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
|
show 2 more comments
string sourc = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\s]*(Summoner[\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
|
show 2 more comments
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
|
show 2 more comments
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
edited Jan 19 at 6:23
answered Jan 19 at 5:01
Anu ViswanAnu Viswan
4,5232524
4,5232524
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
|
show 2 more comments
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
thank you for your answer so very much. Would you be so kind as to answer the following question? I want it to be in the same line like Summoner1|Summoner2|Summoner3|Summoner4 to be able to use regex multiple matches.
– XHSKR
Jan 19 at 5:51
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
you mean the input string would be in mentioned format or result ?
– Anu Viswan
Jan 19 at 5:53
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
yeah the latter one. the result.
– XHSKR
Jan 19 at 5:57
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
@XHSKR I have updated the answer for you, you can use string.Join for the purpose.
– Anu Viswan
Jan 19 at 6:00
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
I have updated the question. it says unable to convert from char to string.
– XHSKR
Jan 19 at 6:13
|
show 2 more comments
string sourc = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\s]*(Summoner[\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();
add a comment |
string sourc = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\s]*(Summoner[\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();
add a comment |
string sourc = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\s]*(Summoner[\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();
string sourc = @"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\s]*(Summoner[\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();
answered Jan 19 at 6:17
windfogwindfog
1898
1898
add a comment |
add a comment |
input.Split(new {':',' '}).First()
– TheGeneral
Jan 19 at 3:51