2020-11-08 09:34:05 +00:00
|
|
|
created: 20201107112846692
|
2021-11-01 13:24:30 +00:00
|
|
|
modified: 20211101125225197
|
2020-11-08 09:34:05 +00:00
|
|
|
tags: [[Operator Examples]] [[search-replace Operator]]
|
|
|
|
title: search-replace Operator (Examples)
|
|
|
|
type: text/vnd.tiddlywiki
|
|
|
|
|
|
|
|
\define myregexp() e|o
|
|
|
|
|
2021-08-29 16:44:34 +00:00
|
|
|
\define myregexp2() ^(?!Unlike).*$
|
|
|
|
|
2020-11-08 09:34:05 +00:00
|
|
|
\define names() (\w+)\s(\w+)
|
|
|
|
|
|
|
|
Replace one string with another:
|
|
|
|
<<.operator-example 1 """[[The quick brown fox jumps over the lazy dog]search-replace[dog],[cat]]""">>
|
|
|
|
|
|
|
|
Replace all matches of a regular expression:
|
|
|
|
|
|
|
|
`\define myregexp() e|o`
|
|
|
|
<<.operator-example 2 """[[Hello There]search-replace:g:regexp<myregexp>,[]]""">>
|
|
|
|
|
|
|
|
Replace all matches of a string irrespective of case:
|
|
|
|
<<.operator-example 3 """[[Hello There]search-replace:gi[H],[]]""">>
|
|
|
|
|
|
|
|
You can also use regular expression capture groups in the replacement string:
|
|
|
|
`\define names() (\w+)\s(\w+)`
|
2020-11-18 15:56:38 +00:00
|
|
|
<<.operator-example 4 """[[John Smith]search-replace::regexp<names>,[$2,$1]]""" >>
|
|
|
|
|
2021-11-01 13:24:30 +00:00
|
|
|
You can reference the portion of the input that matches the regular expression with `$&`:
|
|
|
|
<<.operator-example 5 """[[John Smith]search-replace::regexp[John .*],[His name is $&]]""">>
|
|
|
|
|
|
|
|
<<.operator-example 6 """[[This is an exciting feature]search-replace::regexp[exciting],[amazing and $&]]""">>
|
|
|
|
|
2021-08-29 16:44:34 +00:00
|
|
|
To replace everything but a match using a regular expression and the ''multiline'' (m) flag:
|
|
|
|
`\define myregexp2() ^(?!Unlike).*$`
|
2021-11-01 13:24:30 +00:00
|
|
|
<<.operator-example 7 """[[HelloThere]get[text]search-replace:gm:regexp<myregexp2>,[]]""">>
|
|
|
|
|
2021-08-29 16:44:34 +00:00
|
|
|
|
2020-11-18 15:56:38 +00:00
|
|
|
{{How to remove stop words}}
|