This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
action:Search_and_Replace [2017/09/07 06:00] peternlewis created |
action:Search_and_Replace [2023/09/12 05:30] (current) peternlewis |
||
---|---|---|---|
Line 3: | Line 3: | ||
Prior to v8.0 there were separate Search and Replace Variable and Search and Replace Clipboard actions and the results always went back to the source variable or clipboard. | Prior to v8.0 there were separate Search and Replace Variable and Search and Replace Clipboard actions and the results always went back to the source variable or clipboard. | ||
- | {{:action:searchreplace.png?nolink&516|}} | + | {{:action:searchreplace.png?nolink|}} |
You can search for: | You can search for: | ||
Line 16: | Line 16: | ||
Enter what to replace the found text with in the //and replace with// text area. Use the arrow at the right of the text area to select variables, clipboards and other tokens to enter in the text area. | Enter what to replace the found text with in the //and replace with// text area. Use the arrow at the right of the text area to select variables, clipboards and other tokens to enter in the text area. | ||
+ | |||
+ | For regular expressions, the replacement can refer to capture groups using either `$1` or `\1` notation (eg `%Calculate%CHARACTERS(\1)%` would be the count of characters in the first capture match (v8+)), or named capture groups (using `(?<name>...)` with the notation `${name}`. | ||
+ | |||
+ | Like other [[/Text_Fields|Text Fields]], you can use [[manual:Tokens#Text_Case_Conversions | text case conversion]] escape sequences like `\U` and `\L` in the replacement field. | ||
Select where you want the result to be saved (back to the source, or to some other variable or clipboard or file). | Select where you want the result to be saved (back to the source, or to some other variable or clipboard or file). | ||
+ | |||
+ | Using the action (gear) ⚙ menu, you can select (v10.0+) to replace all, or only the first or last match. | ||
Examples: | Examples: | ||
Line 25: | Line 31: | ||
Search for: (.+) (.+) | Search for: (.+) (.+) | ||
Replace with: $2 $1 | Replace with: $2 $1 | ||
- | |||
{{:action:search-replace-example.png?nolink&516|}} | {{:action:search-replace-example.png?nolink&516|}} | ||
Line 32: | Line 37: | ||
{{:action:search-replace-example-result.png?nolink&511|}} | {{:action:search-replace-example-result.png?nolink&511|}} | ||
+ | |||
+ | Note that you can use tokens in the replace field (eg `%Variable%Whatever%`), but that when those tokens are expanded, the contents will themselves not be further expanded, so, for example you could not have a variable that contains `$1` and expect that to be replaced by a the corresponding capture group. You could use an AppleScript like this: | ||
+ | |||
+ | ```applescript | ||
+ | tell application "Keyboard Maestro Engine" | ||
+ | set src to getvariable "Source" | ||
+ | set s to getvariable "Search" | ||
+ | set r to getvariable "Replace" | ||
+ | set o to search src for s replace r with regex | ||
+ | end tell | ||
+ | ``` | ||
===== See Also ===== | ===== See Also ===== | ||
Line 37: | Line 53: | ||
=== Actions === | === Actions === | ||
+ | * [[action:Search_using_Regular_Expression|Search using Regular Expression]] action | ||
* [[:Actions|See all Actions]] | * [[:Actions|See all Actions]] | ||
Line 48: | Line 65: | ||
- [[https://forum.keyboardmaestro.com/search?q=Search%20 Replace |Keyboard Maestro Forum topics about Search Replace]] | - [[https://forum.keyboardmaestro.com/search?q=Search%20 Replace |Keyboard Maestro Forum topics about Search Replace]] | ||
- |