{"id":1739,"date":"2003-07-09T22:18:33","date_gmt":"2003-07-09T22:18:33","guid":{"rendered":"http:\/\/www.soulhuntre.com\/items\/date\/2003\/07\/09\/keepin-me-regular\/"},"modified":"2003-07-09T22:18:33","modified_gmt":"2003-07-09T22:18:33","slug":"keepin-me-regular","status":"publish","type":"post","link":"http:\/\/legacyiamsenseiken.local\/2003\/07\/09\/keepin-me-regular\/","title":{"rendered":"Keepin’ me regular…"},"content":{"rendered":"\n\n\n\n
<\/a> <\/td>\n<\/tr>\n
rexeg = leet!<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Well, I finally got it nailed… I think. I have been working on a [[wp:wiki|Wiki]] for our own CMS and as part of the effort for client “R”. The core of this system is the ability to translate specially formatted tags into links as the data is pulled from the database. I use a variation of this format here on this weblog.<\/p>\n

For instance, the link to the term “Wiki” above was actually formatted like this:<\/p>\n

\n
\n
[ [wp:wiki|Wiki] ]  <- take note that the ]] aren’t next to each other \ud83d\ude42<\/i> <\/dt>\n<\/dl>\n<\/blockquote>\n

That means that I want to pass the term<\/u> ‘wiki’ in the namespace<\/u> ‘wp’ and the linked word will actually be ‘Wiki’. The namespace for ‘wp’ (wikipedia) triggers a driver routine that knows how to handle that type of link and whammo, a link to the [[wp:Wikipedia]].<\/p>\n

<\/p>\n

Anyway, I have obviously wound up working on the right way to handle this type of thing, and clearly it was a job for [[wp:regular_expression|regular expressions]] in my mind. I went ahead and did some research<\/a> and found out that .Net<\/a> has some really cool and powerful regex routines<\/a> including powerful classes<\/a>. For my purposes, in addition to handling the parsing, I can insert my own code to handle the matches<\/a> and thus my issue of interfacing to a database is solved.<\/p>\n

I spent a lot of time on and off over the last months learnign about how regexes work, using them in projects and so on. For all that tiem an invaluable addition to my life is Expresso<\/a> – an interactive regex workbench that lets me see what’s happening every step of the way. This thing rocks.<\/p>\n

If your interested, here is the expression I am using, if you can show me a better way please [[let me know]]:<\/p>\n

\n

[[((?<Namespace>.+):)*(?<Term>.+?)(|(?<Alias>.+))*]]<\/p>\n<\/blockquote>\n

And it correctly parses these test cases – mind the extra space between the [ [ to avoid tripping my parser here, take that out if your gonna try these at home:<\/p>\n

\n

[ [_name_ _name_:_term_ _term_|_alias_ _alias_] ]
[ [_term_ _term_|_alias_ _alias_] ]
[ [_name_ _name_:_term_ _term_] ]
[ [_term_ _term_] ]<\/p>\n<\/blockquote>\n

The only annoyance is I wind up with two extra capture groupings that aren’t named. I am sure there is a way to avoid that in the regex itself, but for not I just turn on the “Explicit Capture” option and all is good. The regex runs, I get a call to my MatchEvaluator<\/a> for each hit and the named groups give me access to what was found. A quick trip to the database to handle some stuff and I can pass back a nicely formatted <A> link that is just what I want.<\/p>\n

A snippit of the actual C# code for this is deal is below:<\/p>\n

\n

using System.Text.RegularExpressions;<\/p>\n

Regex regex = new Regex(
@”[[((?<Namespace>.+):)*(?<Term>.+?)(|(?Alias>.+))*]]”,
RegexOptions.Multiline | RegexOptions.ExplicitCapture
);<\/p>\n<\/blockquote>\n

Features I want to add? So far, the only one I am looking for is a way to escape the [[ pairs. But to be honest I think [[ would do it … I just want it “officially” in the regex.<\/p>\n","protected":false},"excerpt":{"rendered":"

rexeg = leet! Well, I finally got it nailed… I think. I have been working on a [[wp:wiki|Wiki]] for our own CMS and as part of the effort for client “R”. The core of this system is the ability to translate specially formatted tags into links as the data is pulled from the database. I […]<\/p>\n","protected":false},"author":3,"featured_media":53173,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[278],"tags":[],"_links":{"self":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/1739"}],"collection":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/comments?post=1739"}],"version-history":[{"count":0,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/1739\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media\/53173"}],"wp:attachment":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media?parent=1739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/categories?post=1739"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/tags?post=1739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}