c# - How to get string value in brackets with regex -
i have seen page asks same question reply given on page not seem work me.
i have text file contains lot of words in square brackets , trying remove brackets. have opened text file , converted string using streamreader class.
when seek display strings,none show up.
can tell me i'm doing wrong here? heres code:
list<string> words = new list<string>(); streamreader sr = new streamreader(file.openread(ofd.filename)); string wordlist = sr.readtoend(); textentry.text = wordlist; matchcollection matches = regex.matches(wordlist, "[(.+?)]", regexoptions.singleline); foreach (match match in matches) { string add together = match.groups[1].value; words.add(add); } foreach (string word in words) { messagebox.show(word); }
any help appreciated,thanks.
you have escape brackets, since preserved keywords in regular expressions. see in code below \
escaped too, since \
has special meaning in c#, escape escape character:
regex.matches(wordlist, "\\[(.+?)\\]", regexoptions.singleline);
or, using verbatim operator:
regex.matches(wordlist, @"\[(.+?)\]", regexoptions.singleline);
c#
No comments:
Post a Comment