Jump to content

Regular expressions: Difference between revisions

Line 1,228:
expression test.1 2 match try-regex .
expression test.2 2 match try-regex .</lang>
 
=={{header|TXR}}==
{{works with|TXR git head}}
 
Txr is not designed for sed-like filtering, but here is how to do <code>sed -e 's/dog/cat/g'</code>:
 
<lang txr>@(collect)
@(coll :gap 0)@mismatch@{match /dog/}@(end)@suffix
@(output)
@(rep)@{mismatch}cat@(end)@suffix
@(end)
@(end)</lang>
 
How it works is that the body of the <code>coll</code> uses a double-variable match: an unbound variable followed by a regex-match variable. The meaning of this combination is, "Search for the regular expression, and if successful, then bind all the characters whcih were skipped over by the search to the first variable, and the matching text to the second variable."
So we collect pairs: pieces of mismatching text, and pieces of text which match the regex <code>dog</code>. At the end, there is usually going to be a piece of text which does not match the body, because it has no match for the regex. Because <code>:gap 0</code> is specified, the coll construct will terminate when faced with this nonmatching text, rather than skipping it in a vain search for a match, which allows <code>@suffix</code> to take on this trailing text.
 
To output the substitution, we simply spit out the mismatching texts followed by the replacement text, and then add the suffix.
 
=={{header|Vedit macro language}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.