XRX/Regular Expression Builder

Motivation
You want to build a form that will allow you to quickly test replacement regular expressions using XQuery's replace function.

Method
We will use an XForms client that has several controls:
 * 1) a test input string
 * 2) a regular expression to evaluate
 * 3) a new string to be used in the replace
 * 4) a "submit" trigger (button)

When the submit trigger is pressed, the three strings are sent to the server and the XQuery replace function will be executed. The result is an output text area that is returned from the server to the web browser.

This tool will allow you to do things such as test your URL-rewrite rules.

Link to Working Application
Replace Test

Sample Program
  @namespace xf url("http://www.w3.org/2002/xforms"); body {font-family: Ariel, Helvetica, san-serif}

/* Input controls appear on their own lines. */ xf|input, xf|select, xf|select1, xf|textarea {display:block; margin:5px 0;}

/* Makes the labels right aligned in a 150px wide column that floats to the left of the input controls. */ xf|input > xf|label, xf|select > xf|label, xf|select1 > xf|label, xf|textarea > xf|label, xf|output > xf|label {font-weight: bold;text-align:right; padding-right:10px; width:150px; float:left; text-align:right;}

/* make the input boxes a little wider */ .xf-value {width: 200px}

   

xquery version "1.0"; declare namespace exist = "http://exist.sourceforge.net/NS/exist"; declare namespace system="http://exist-db.org/xquery/system"; declare namespace request="http://exist-db.org/xquery/request"; declare option exist:serialize "method=xml media-type=text/xml indent=yes"; (: replace demo :) let $input := request:get-parameter('input', '') let $pattern := request:get-parameter('pattern', '') let $replacement := request:get-parameter('replacement', '')

return {$input } {$pattern} {$replacement} {replace($input, $pattern, $replacement)}

Discussion
This shows that you can quickly build tools to teach yourself complex functions like regular expression handling. You can also use the XQuery match function, which returns true or false if the regular expression matches an input string.

There are two variations of this example that are interesting. The first is where you replace the input form with a large text area for doing global replacements of large blocks of text. The second is where you replace the input box with a selection list with common replacement patterns.