Skip to content

replace en

Hamatoma edited this page Aug 7, 2022 · 1 revision

This can be used to replace search patterns. Strings or regular expressions can be used as search patterns. With one command, many search patterns can be provided with different values ​​at once.

With regular search patterns, part of the hit can be used in the replacement.

Table of Contents

Parameters

Surname Type Description Example
append bool the result is appended in the destination buffer app
input bufferName The text from this buffer is examined. Default: input input=data
output bufferName the result is written to this buffer. Default: output output=result
meta character Delimiters for the references within the replacement pattern (groups). Default: % meta=%
What patternList A list with alternating search pattern and replacement value, starting with Separator (Autoseparator).
Pattern is a regular expression, a regular string, or a case-insensitive string.
What=|":id"|"19429"|":date"|"2021-06-27"
What=;r/<(name|id)>(.*?)</(name|id)>/;"%group1%: %group2%";"<br/>";""

References for Substitute Values ​​

References to the search pattern can be specified for the substitute value:

  • For normal strings and regular expressions: %group0% (or %0% for short), meaning the search string.
  • Regular expressions only: %groupN% (or %N% for short). N stands for a number (1, 2, ...) and references the match of the Nth bracket in the search expression.
  • The delimiter of the groups (in the example "%") can be redefined with the parameter meta.
Example with normal string:
What=;"Long sentence with many words.";"%0 %0 %0"
Here each hit found is repeated three times.
Example with regular expressions:
What=;r/<(name|id)>(.*?)</(name|id)>/;"%group1%: %group2%";R/<br/?>/;""
Description of the search terms:
  • Search '<' followed by "name" or "id" (defined as group 1 because first bracket), followed by '">" followed by anything String without the following expression (because of the "*?") (defined as group 2 because of the 2nd bracket), followed by "<" and "/", followed by "name" or " id" (defined as group 3, since the 3rd bracket).
  • Replace the pattern above with the first group (i.e. "name" or "id").
  • Search for "<br" followed by "/" or not (because of the '?') followed by ">".
  • Replace this pattern with nothing (there is nothing between the separator ";" and the end of the string ".
The buffer in which the replacement takes place says:
<name>Adam</name></br>
<id>27</name><br>
The result buffer then contains:
name: Adam
ID: 27

Examples

Example 1: Replacing named parameters in SQL statement

The sql buffer contains:

SELECT
  bill_no,
  (SELECT sum(price) FROM billitems bi
    WHERE bb.billitem_bill=bi.bill_id
      AND billitem_date = :date
  ) as price_sum
FROM bill bb
WHERE
  bb.bill_name like :name AND bill_created = :date
;
Command:
replace What=;/:name/;'Miller';/:date/;"'2021-06-02'" input=sql
The result buffer output (default setting) contains:
SELECT
  bill_no,
  (SELECT sum(price) FROM billitems bi
    WHERE bb.billitem_bill=bi.bill_id
      AND billitem_date = '2021-06-02'
  ) as price_sum
FROM bill bb
WHERE
  bb.bill_name like 'Miller' AND bill_created = '2021-06-02'
;

Example 2: Swapping columns in a CSV file

The input buffer contains:

name;id;year
Adam;10;1970
Berta;11;2003
Command:
replace What=|r/(.*?);(.*?);(.*)/|"%2%;%3%;%1%"
The result buffer output (default setting) contains:
id;year;name
10;1970;Adam
11;2003;Berta

Clone this wiki locally