User Tools

Site Tools


Regular_Expressions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Regular_Expressions [2019/02/03 15:52] – [Online Primers & Tutorials] ccstoneRegular_Expressions [2026/10/08 02:41] (current) – peternlewis
Line 1: Line 1:
-{{:regex-icon.png?nolink |}} 
 ====== Regular Expressions (RegEx) ====== ====== Regular Expressions (RegEx) ======
  
- +Keyboard Maestro uses [[https://www.keyboardmaestro.com/action/linkthru?regularexpressionreferenceoverview|ICU Regular Expressions]] (aka RegEx or RegExp) which is very similar to PCRE (Perl Compatible Regular Expressions), and you can read their [documentation](https://www.keyboardmaestro.com/action/linkthru?regularexpressionreference) by choosing ICU Regular Expression Reference from the Help menu in Keyboard Maestro.  
-Keyboard Maestro uses [[http://userguide.icu-project.org/strings/regexp|ICU Regular Expressions]] (aka RegEx or RegExp) which is very similar to PCRE (Perl Compatible Regular Expressions), and you can read their [documentation](http://userguide.icu-project.org/strings/regexp#TOC-Regular-Expression-Metacharacters) by choosing ICU Regular Expression Reference from the Help menu in Keyboard Maestro.  +
  
  
Line 19: Line 17:
 ===== Search Modifiers ===== ===== Search Modifiers =====
  
-The ICU calls these modifiers "[[http://userguide.icu-project.org/strings/regexp#TOC-Flag-Options|flag options]]".+The ICU calls these modifiers [[https://www.keyboardmaestro.com/action/linkthru?regularexpressionreferenceflags|flag options]].
  
-The search modifier "Pattern to Use" shown below is placed at the very beginning of the Search/Find Regular Expression box.\\   
-For example:\\  
-`(?m)^\s*\d+[\t]+`\\ 
-as shown in the below example #1. 
  
 ^Purpose^Pattern to Use^Description^ ^Purpose^Pattern to Use^Description^
-|Global|NONE|All searches are global in Keyboard Maestro.\\ This is often indicated in other tools by the `/g` modifier.|+|Global|NONE|The [[action:Search_using_Regular_Expression|Search using Regular Expression Action]] will return the _first_ match it finds in the source string.\\  In order to make it be a "global" search, you need to put this Action in a [[action:For_Each|For Each Action]] that uses the [ [[collection:Substrings_In|Substrings Matching in]] ] [[:Collections|Collection]].  This will loop through all matches found in the source string.\\ This is often indicated in other tools by the `/g` modifier.|
 |Case Insensitive|`(?i)`|Matching will be in a case-insensitive manner.\\  In some Actions this is NOT necessary since the Action already provides an "ignoring case" option.| |Case Insensitive|`(?i)`|Matching will be in a case-insensitive manner.\\  In some Actions this is NOT necessary since the Action already provides an "ignoring case" option.|
 |Dot includes EOL|`(?s)`|A "`.`" in a pattern will match a line terminator in the input text. Note that a CR LF pair in text behave as a single line terminator, and will match a single "." in a RE pattern.| |Dot includes EOL|`(?s)`|A "`.`" in a pattern will match a line terminator in the input text. Note that a CR LF pair in text behave as a single line terminator, and will match a single "." in a RE pattern.|
Line 33: Line 27:
 |Word Boundaries|`(?w)`|Controls the behavior of \b in a pattern.\\ If used, word boundaries are found according to the definitions of word found in [[http://unicode.org/reports/tr29/#Word_Boundaries|Unicode UAX 29, Text Boundaries]].| |Word Boundaries|`(?w)`|Controls the behavior of \b in a pattern.\\ If used, word boundaries are found according to the definitions of word found in [[http://unicode.org/reports/tr29/#Word_Boundaries|Unicode UAX 29, Text Boundaries]].|
 |Comments|`(?x)`|Ignores white space and allows #comments.\\ If used, white space within regular expressions is ignored and you can use #line-comments.| |Comments|`(?x)`|Ignores white space and allows #comments.\\ If used, white space within regular expressions is ignored and you can use #line-comments.|
 +
 +Note that matches are **case sensitive** by default (unless the action offers an //ignores case// option), so you can use (?i) to make the regular expression case insensitive.
 +
 +The search modifier can apply to a section of the regular expression by using the form:
 +
 +```
 +Case Sensitive (?i:case insensitive) Case Sensitive
 +```
 +
 +of the search modifier can apply for the remainder of the pattern (unless changed later) like this:
 +
 +```
 +Case Sensitive (?i)case insensitive case insensitive
 +```
 +
 +or
 +
 +```
 +Case Sensitive (?i)case insensitive (?-i) Case Sensitive
 +```
 +
 +For example to search for the start of a line anywhere within the pattern, for the text "Hello THERE" where THERE is case insensitive you could use:
 +
 +```
 +(?m)^Hello(?i:there)
 +```
 +
 +Note that `(?…)` is not a Capture Group.
  
  
Line 39: Line 61:
   * Use the form of `$<CG#>`, or `\<CG#>` (in ver 8+) in the Action Replace box, where `<CG#>` is the number for the capture group.    * Use the form of `$<CG#>`, or `\<CG#>` (in ver 8+) in the Action Replace box, where `<CG#>` is the number for the capture group. 
   * Examples:    * Examples: 
-    * `$1, $2, $3` +    * `$1`, `$2`, `$23` 
-    * `\1, \2, \3`+    * `${1}`, ${2}`, ${23}`,  
 +    * `${name}` - named capture group (v9.2+, 10.13+) 
 +    * `\1`, `\2`, `\3` - single digit only (v8.0+)
   * This is the same as `\<CG#>` used in other apps/languages, like BBEdit.   * This is the same as `\<CG#>` used in other apps/languages, like BBEdit.
 +  * The zeroth capture group (eg `$0`) is the entire match.
  
 For more information, see [[https://www.regular-expressions.info/brackets.html | Capture Groups]]. For more information, see [[https://www.regular-expressions.info/brackets.html | Capture Groups]].
Line 53: Line 78:
 ===== ICU 55+ Metacharacters ===== ===== ICU 55+ Metacharacters =====
  
-For the currently complete list, see [ICU Regular Expression Metacharacters](http://userguide.icu-project.org/strings/regexp#TOC-Regular-Expression-Metacharacters). +For the currently complete list, see [ICU Regular Expression Metacharacters](https://www.keyboardmaestro.com/action/linkthru?regularexpressionreference).
- +
-**New Metacharacters Available in ICU 55+**(([ICU Regular Expression Metacharacters](http://userguide.icu-project.org/strings/regexp#TOC-Regular-Expression-Metacharacters)))+
  
 [ICU 55](http://site.icu-project.org/download/55) was released on 2015-04-01, and is available in the follow software versions: [ICU 55](http://site.icu-project.org/download/55) was released on 2015-04-01, and is available in the follow software versions:
Line 63: Line 86:
 These Metacharacters offer some powerful solutions to long-standing RegEx problems. These can replace complicated RegEx patterns previously required, and are recommended for use if you are running the required versions.  For example, `\h` and `\R`.  ICU 55 is _not_ available in all RegEx engines.  A notable exception is JavaScript (and JXA), even in High Sierra.  So if you are using the native Keyboard Maestro Actions that use RegEx, it would be available, but not necessarily in Actions that use Execute Script in another language.  It is available in Execute AppleScript Actions that use ASObjC RegEx. These Metacharacters offer some powerful solutions to long-standing RegEx problems. These can replace complicated RegEx patterns previously required, and are recommended for use if you are running the required versions.  For example, `\h` and `\R`.  ICU 55 is _not_ available in all RegEx engines.  A notable exception is JavaScript (and JXA), even in High Sierra.  So if you are using the native Keyboard Maestro Actions that use RegEx, it would be available, but not necessarily in Actions that use Execute Script in another language.  It is available in Execute AppleScript Actions that use ASObjC RegEx.
  
-^   New Metacharacters in ICU 55+(([ICU Regular Expression Metacharacters](http://userguide.icu-project.org/strings/regexp#TOC-Regular-Expression-Metacharacters)))   ^^^+^   New Metacharacters in ICU 55+(([ICU Regular Expression Metacharacters](https://www.keyboardmaestro.com/action/linkthru?regularexpressionreference)))   ^^^
 ^ Character ^ Alternate Expression\\ (Pre ICU 55) ^ Description ^ ^ Character ^ Alternate Expression\\ (Pre ICU 55) ^ Description ^
 | `\h` | `[^\S\r\n\f]` | Match a Horizontal White Space character.\\ They are characters with Unicode General Category of Space_Separator plus the ASCII tab (\u0009). | | `\h` | `[^\S\r\n\f]` | Match a Horizontal White Space character.\\ They are characters with Unicode General Category of Space_Separator plus the ASCII tab (\u0009). |
Line 82: Line 105:
 Sometimes you may need to extract a RegEx Capture Group after a match is made, and do this for multiple lines (matches) in the entire source string.  To achieve this, you need to: Sometimes you may need to extract a RegEx Capture Group after a match is made, and do this for multiple lines (matches) in the entire source string.  To achieve this, you need to:
  
-1. Use the [[action:For_Each|For Each Action]] to get each match into a variable (MatchString) +1. Use the [[action:For_Each|For Each Action]] with the [[collection:Substrings_In|Substrings In]] collection to get each match into a variable (Local MatchString). 
-2. Then use a [[action:Search_Variable|Search Variable Action]] to get the Capture Group for that match+2. Then use a [[action:Search_using_Regular_Expression|Search using Regular Expression]] action to get the Capture Group for that match.
  
 Both Actions can use the same RegEx pattern. Both Actions can use the same RegEx pattern.
  
-{{:km-for-each-regex-example.png?nolink|}}\\ 
  
-For a complete macro using this method, see:\\ +{{:km-for-each-regex-example.png?nolink|}} 
-[MACRO: Get List of RegEx Capture Group of Multiple Matches](https://forum.keyboardmaestro.com/t/macro-get-list-of-regex-capture-group-of-multiple-matches/2975)+ 
 +For a complete macro using this method, see: [MACRO: Get List of RegEx Capture Group of Multiple Matches](https://forum.keyboardmaestro.com/t/macro-get-list-of-regex-capture-group-of-multiple-matches/2975)
  
  
Line 132: Line 155:
 ==== General ==== ==== General ====
  
-  - [[http://userguide.icu-project.org/strings/regexp|ICU Regular Expressions]]+  - [[https://www.keyboardmaestro.com/action/linkthru?regularexpressionreferenceoverview|ICU Regular Expressions]]
   - [ICU Unicode Properties for Regular Expressions](http://unicode.keyboardmaestro.com)   - [ICU Unicode Properties for Regular Expressions](http://unicode.keyboardmaestro.com)
   - [[https://en.wikipedia.org/wiki/Regular_expression|Wikpedia Regular Expression article]]   - [[https://en.wikipedia.org/wiki/Regular_expression|Wikpedia Regular Expression article]]
Line 138: Line 161:
 ==== Software ==== ==== Software ====
  
-  - [[http://www.barebones.com/products/bbedit/|BBEdit (commercial) or BBEdit-Lite (freeware text editor with PCRE regular expression support)]] +  * [[http://www.barebones.com/products/bbedit/|BBEdit]] -- A Programming Editor with PCRE regular expression support.      
-  - [[http://itunes.apple.com/us/app/patterns-the-regex-app/id429449079?mt=12|Patterns — a regular expression analyzer on the app-store]] +    * The commercial demo expires in 30 days and reverts to the still //very// powerful BBEdit-Lite (freeware). 
-  - [[https://itunes.apple.com/us/app/regexrx/id498370702?mt=12|RegExRX — a regular expression analyzer on the app-store]] +'''' 
-  - [[https://regex101.com|RegEx101.com]] -- Online RegEx Analyzer+ 
 +  * [[http://itunes.apple.com/us/app/patterns-the-regex-app/id429449079?mt=12|Patterns]] -- A regular expression analyzer available on the app-store. 
 +  * [[https://itunes.apple.com/us/app/regexrx/id498370702?mt=12|RegExRX]] -- A regular expression analyzer available on the app-store. 
 + 
 +  '''' 
 + 
 +  * [[https://regex101.com|RegEx101.com]] -- An Outstanding and Comprehensive Online RegEx Analyzer. 
 +  * [[http://regexr.com/|Regexr]] -- Regex testing and explaining, examples and references.
  
 ==== Books ==== ==== Books ====
Line 157: Line 187:
 ==== Online Primers & Tutorials ==== ==== Online Primers & Tutorials ====
  
-  - [[http://www.agillo.net/regex-primer-part-1/|Regex Primer : Part 1]] 
-  - [[http://www.agillo.net/regex-primer-part-2/|Regex Primer : Part 2]] 
   - [Regular Expressions Quick Start](http://www.regular-expressions.info/quickstart.html)   - [Regular Expressions Quick Start](http://www.regular-expressions.info/quickstart.html)
   - [[http://www.codeproject.com/KB/dotnet/regextutorial.aspx|CodeProject.com RegExTutorial]]   - [[http://www.codeproject.com/KB/dotnet/regextutorial.aspx|CodeProject.com RegExTutorial]]
  
 Keywords:  //Regular Expression, RegEx, RegExp, Find, Replace, Match// Keywords:  //Regular Expression, RegEx, RegExp, Find, Replace, Match//
Regular_Expressions.1549209178.txt.gz · Last modified: by ccstone

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki