2

Hi I need to find all matches in a large string, I found this question

Detect particular tokens in a string. C#

Which seemed perfect! Only problem it didn't work. The difference between that problem and mine is that instead of using #\w+# I need to use [[\w+]]. Here is the code I have (sb is my large string):

        Regex reg = new Regex(@"[[\w+]]");
        foreach (Match m in reg.Matches(sb.ToString()))
        {
            RenderHtmlRecursive(helper, sb, path, m.Value);
        }

When I run this I get 1 match :) but the value is (m.Value): "t]"

sb is (small text for now): Card card [[RegisterText]]

Any ideas?

Community
  • 1
  • 1
Paul
  • 1,457
  • 1
  • 19
  • 36

3 Answers3

5

[ and ] are special characters used in Regular Expressions, and they need to be escaped if you wish to match that character.

Simply changing it to the following should work;

Regex reg = new Regex(@"\[\[\w+\]\]");
melwil
  • 2,547
  • 1
  • 19
  • 34
3

This would be an excellent opportunity to use a DAFSA which is a form of a Trie which has no payload. Efficient, O(n) lookup for phrases in large strings.

Haney
  • 32,775
  • 8
  • 59
  • 68
-2

You may considered named token in regex. For example ((?((POBox|PO\sBox)\s*\d*)),?\s?)?(((?([\w\d\s\,])),\s?)?( (?([\w\s-]\w\s(st\s)?[\w]\s(street|st|road|rd|close|cl|avenue|ave|av|path|ph|drive|drv|LOOP|COURT|CT|CIRCLE|LANE|LN)) ),?\s?))?((?([\p{Ll}\p{Lu}\p{Lo}\p{Pc}\p{Lt}\p{Lm}\s])),?\s?)?((?(Victoria|VIC|New South Wales|NSW|South Australia|SA|Northern Territory|NT|West Australia|WA|Tasmania|TAS|ACT|Queensland|QLD))\s*)?(?(\d{4}),?\s?)?(?(Australia))?

for 28 Lidco Street, Arndell Park, Sydney 2148

To evaluate your regx expression, it is handy to use Expresso - A Tool for Building and Testing Regular Expressions at http://www.codeproject.com/Articles/3669/Expresso-A-Tool-for-Building-and-Testing-Regular-E

ZZZ
  • 2,752
  • 2
  • 25
  • 37
  • 2
    It is exceedingly unclear to me how your answer has anything at all to do with the OP's question aside from the fact that regexes are involved. – Kirk Woll May 27 '13 at 00:32