XRX/正则表达式构建器
外观
< XRX
您想要构建一个表单,允许您使用 XQuery 的 replace 函数快速测试替换正则表达式。
我们将使用一个 XForms 客户端,它具有多个控件
- 一个测试输入字符串
- 一个要评估的正则表达式
- 一个要在替换中使用的新的字符串
- 一个“提交”触发器(按钮)
当按下提交触发器时,这三个字符串将被发送到服务器,XQuery replace() 函数将被执行。结果是一个输出文本区域,它从服务器返回到 Web 浏览器。
此工具将允许您执行诸如测试您的 URL 重写规则之类的事情。


<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ev="http://www.w3.org/2001/xml-events" >
<head>
<style type="text/css">
@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}
</style>
<xf:model>
<xf:instance id="request" xmlns="">
<data>
<input>abcdefghi</input>
<pattern>def</pattern>
<replacement>123</replacement>
</data>
</xf:instance>
<xf:instance id="response" xmlns="">
<data/>
</xf:instance>
<xf:submission id="submit" method="get"
action="https://:8080/exist/rest/db/test/replace/replace.xq"
replace="instance" instance="response"
separator="&">
<xf:toggle case="case-busy" ev:event="xforms-submit" />
<xf:toggle case="case-submit-error" ev:event="xforms-submit-error" />
<xf:toggle case="case-done" ev:event="xforms-submit-done" />
</xf:submission>
</xf:model>
</head>
<body>
<h1>XForms Replace Tester</h1>
<xf:input ref="input">
<xf:label>Input:</xf:label>
</xf:input>
<xf:input ref="pattern">
<xf:label>Pattern:</xf:label>
</xf:input>
<xf:input ref="replacement">
<xf:label>Replacement:</xf:label>
</xf:input>
<xf:switch>
<xf:case id="ready">
<xf:submit submission="submit">
<xf:label>Submit</xf:label>
</xf:submit>
<xf:submit submission="echo-test">
<xf:label>Echo Test</xf:label>
</xf:submit>
</xf:case>
<xf:case id="case-busy">
<p>Waiting for response...</p>
</xf:case>
<xf:case id="case-submit-error">
<p>The server has returned a submit error event.</p>
</xf:case>
<xf:case id="case-done">
<xf:output ref="instance('response')/replace-result/text()">
<xf:label>Result:</xf:label>
</xf:output>
</xf:case>
</xf:switch>
</body>
</html>
如果您使用的是 eXist,只需将此文件放在服务器上,与您的 XForms 测试驱动程序在同一个文件夹中。
在上面的示例中,我使用了 localhost 上的一个测试文件夹
https://:8080/exist/rest/db/test/replace/replace.xq
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
<results>
<input>{$input }</input>
<pattern>{$pattern}</pattern>
<replacement>{$replacement}</replacement>
<replace-result>{replace($input , $pattern, $replacement)}</replace-result>
</results>
这表明您可以快速构建工具来学习诸如正则表达式处理之类的复杂函数。您还可以使用 XQuery 的 match 函数,该函数如果正则表达式与输入字符串匹配则返回 true 或 false。
此示例有两个有趣的变体。第一个是您用一个大的文本区域替换输入表单以对大量文本块执行全局替换。第二个是您用一个包含常见替换模式的选择列表替换输入框。
替换函数的示例可以在此处找到