c# - Regex finding any characters in between ( ) -
i have long text , in text there many ( hello , hi ) or (hello,hi) , have take space account . how detect them in long text , retrieve hello , hi word , add list text? use regex :
string helpingwordpattern = "(?<=\\()(.*?)(?<=\\))"; regex regexhelpingword = new regex(helpingwordpattern); foreach (match m in regexhelpingword.matches(lstquestion.questioncontent)) { // removing "," , store helping word list string str = m.tostring(); if (str.contains(",")) { string[] strwords = str.split(','); // contain ) word , e.g. ( whole) ) if(strwords.contains(")")) { strwords.replace(")", ""); // try remove them. error here cos can't use array replace. } foreach (string words in strwords) { options.add(words); } } } i google , search correct regex , regex use suppose remove ) doesn't .
you can use regex pattern:
(?<=[\(,])(.*?)(?=[\),]) (?<=[\(,])(\d*?)(?=[\),]) // except number break up:
(?<=[\(,]) = positive behind, looks `(`or `,` (.*?) = looks thing except new line, lazy(matches less possible) (?=[\),]) = positive ahead, looks `)` or `,` after `hello` or `hi` etc. demo
edit
you can try sample code achievement: (untested)
list<string> lst = new list<string>(); matchcollection mcoll = regex.matches(samplestr,@"(?<=[\(,])(.*?)(?=[\),])") foreach(match m in mcoll) { lst.add(m.tostring()); debug.print(m.tostring()); // optional, check in output window. }
Comments
Post a Comment