c# - Regex Expression Incorrect Output -
hey guys i'm trying create regex function in c# search sentence words ending in "es". i'm trying output in format of
word1 = bes word2 = mes word3 = ces and on. problem keep getting "errorcs0136" upon compilation. tried using console.writeline() , having counter variable increment on each iteration of loop wouldn't work. here copy of errors i'm getting. much.
task1.cs(66,52): error cs1026: unexpected symbol ',', expecting ')'
task1.cs(66,55): error cs0136: local variable named 'match' cannot declared in scope because give different meaning 'match', used in 'parent or current' scope denote else
task1.cs(66,59): error cs1026: unexpected symbol ')', expecting ')'
compilation failed: 3 error(s), 0 warnings
below source code.
public void numpatternsearch(){ string input3; string pattern = @"\b\w+es\b"; //regex regex = new regex("[*]"); console.writeline("enter string search: "); input3 = console.readline(); //input3 = string.join("", input3.where(char.isdigit).toarray()); //input3 = regex.match(input3, @"\d+").value; //string[] substrings = regex.split(input3); foreach (match match in regex.matches(input3, pattern)){ int count = 1; string[] substrings = "number"+count+" = '{0}'", match; count++; console.writeline(substrings); } //console.writeline(input3); } }
your compiler error occurs in line:
string[] substrings = "number"+count+" = '{0}'", match; that's not valid c#. if want list of resulting strings, can try this:
list<string> substrings = new list<string>(); int count = 1; foreach (match match in regex.matches(input3, pattern)) { string substring = string.format("number{0} = '{1}'", count, match); count++; console.writeline(substring); substrings.add(substring); } you can use substrings further operations. can convert list<string> array using
string[] substringarray = substrings.toarray(); note declared count outside loop. otherwise use 1 count!
Comments
Post a Comment