1

I have string:

string mystring = "hello(hi,mo,wo,ka)";

And i need to get all arguments in brackets. Like:

hi*mo*wo*ka

I tried that:

string res = "";
string mystring = "hello(hi,mo,wo,ka)";
mystring.Replace("hello", "");
string[] tokens = mystring.Split(',');
string[] tokenz = mystring.Split(')');
foreach (string s in tokens)
{
     res += "*" + " " + s +" ";
}
foreach (string z in tokenz)
{
     res += "*" + " " + z + " ";
}
return res;

But that returns all words before ",".

(I need to return between

"(" and ","

"," and ","

"," and ")"

)

Matthias
  • 4,481
  • 12
  • 45
  • 84
Qrai
  • 31
  • 1
  • 6

3 Answers3

1

You can try to use \\(([^)]+)\\) regex get the word contain in brackets,then use Replace function to let , to *

string res = "hello(hi,mo,wo,ka)";

var regex =  Regex.Match(res, "\\(([^)]+)\\)");
var result = regex.Groups[1].Value.Replace(',','*');

c# online

Result

hi*mo*wo*ka
D-Shih
  • 44,943
  • 6
  • 31
  • 51
0

This way :

  Regex rgx = new Regex(@"\((.*)\)");
  var result = rgx.Match("hello(hi,mo,wo,ka)");
Mojtaba Tajik
  • 1,725
  • 16
  • 34
0

Split method has an override that lets you define multiple delimiter chars:

string mystring = "hello(hi,mo,wo,ka)";
var tokens = mystring.Replace("hello", "").Split(new[] { "(",",",")" }, StringSplitOptions.RemoveEmptyEntries);  
qbik
  • 5,502
  • 2
  • 27
  • 33