Version 11 (modified by dfaerch, 10 years ago) (diff)


Include and do Regex-replacements


This is a hack upon the Include macro, that adds regular expression replace functionality.

Very usable when you whish to extract data from and/or change data inside an included page.


  • arg 1 : url - Like the Include macro except, local files cannot be included1. Also accepts wiki:MyPage
  • arg 2 : format. Either "wiki" or "raw", where wiki does WikiFormatting
  • arg 3+: "Control arguments". See Control Arguments below.
  • arg last:The regular expressions MUST be the last argument

Regular expressions are defined as such:
You can use several expressions:

You can also use capture groups. In python these are defined as \1,\2 ect. (where in eg. perl its $1,$2)

Control Arguments

Control Arguments are defined AFTER the second argument (raw,wiki) and BEFORE the regular expressions. Multiple arguments are seperated by comma.

If defined, replace $USER with username in URL. (NOT in the included text!).
use_vars accepts a sub-argument, used change the case of the username. This is one of:
  • upper
  • lower
  • ucfirst
example: use_vars=upper

(note: ucfirst is "uppercase first letter only")

Do not include this page for anonymous users.
NOTE: this is NOT a security feature, as anyone can still read your source code.
Good feature to not include unneseseary information for anon users.
Disable the use of DOTALL option for regular expression.
pr. default DOTALL is enabled and this make the dot (".") also include linebreaks

  1. 1. Well, they can if the file resides in /tmp/trac_include/ and no slashes are used in filename.

Bugs/Feature Requests

Existing bugs and feature requests for RegexIncludeMacro are here.

If you have any issues, create a new ticket.


Download the zipped source from [download:regexincludemacro here].


You can check out RegexIncludeMacro from here using Subversion, or browse the source with Trac.


A simple example. Replace all occurences of the word "windows" with the word "Linux"


The next example shows how to use the Control Argument use_vars to create a personalized start page on the wiki:


In my case, it would include a page from the wiki called "!StartPage_dfaerch". (and always lowercasing the username). And for anonymous users, it would include "!StartPage_anonymous", unless "no_anon" is defined as well.

The last example includes and wiki-formats the wanted data, and removes the unwanted. Perticulary, i want to only keep the major version information, not patches nor snapshots.

Note how i use 2 regular expression replaces, seperated by ','. The first expression formats the lines i want to keep, the second deletes the rest.

[[RegexInclude(,wiki,'The latest ([\w.]+) version [^:]+:\s*([a-z0-9.-]+)'/' '''\1''' :: \n  ''\2'' ','\nThe[ a-zA-Z0-9.:-]+'/'\n')]]

The result is:



Known limitations

  • expressions or replacement-strings cannot contain ',' (that is: tick-comma-tick) since that combination is used for splitting the expressions:
  • expressions or replacement-strings cannot contain '/' (that is: tick-slash-tick) since that combination is used for splitting the expression from replacement-string.

I hope these two combinations are so unlikely, that the limitation wont cause any trouble.

  • url's cannot contain comma. If you need comma, you can URLEncode it. (as such: /file?arg=1,2 = /file?arg=1%2c2)

Recent Changes

1357 by dfaerch on 2006-10-09 11:53:17

Added regex match functionallity + match_seperator

1342 by dfaerch on 2006-10-05 15:38:53

added Control Arguments & wiki-page inclusion

1338 by dfaerch on 2006-10-04 16:16:19

initial release



Author: dfaerch