Regular Expressions Support



RE-MATCH: "text" "/regexp/"

This words returns TRUE if it finds a matching string is found when comparing argument text with a regular expression.

Example:

#( test_re_match
\ with the help of a regular expression, let's find 
\ in directory  'c:\xxx' all the files whose names start   
\ with characters  'vk_'  followed by 3 digits, 
\ e.g., 'vk_123test.txt', 'vk_324.gif', vk_647999.pdf' etc.
NoActive
Action:
    FOR-FILES: "c:\xxx\*"
        RE-MATCH: "%FOUND-FILENAME%" "/vk_\d\d\d.*/i"
        IF
            \ ... performing the required actions
        THEN 
    ;FOR-FILES
)#

A postfix version of this word also exists:

S" text" S" /regexp/" RE-MATCH

Detailed description of regular expressions' syntax can be found here.

See also Notes.


RE-ALL: "text" "/regexp/" <...> ;RE-ALL

This words is used to execute a loop for each match found. The loop will be repeated as many times as there are matches.

Example:

#( test_re-all
\ word "nnCron" contains 3 instances of "n",
\ therefore, the loop will be executed 3 times
NoActive
Action:
    RE-ALL: "nnCron" "/n/"
        MSG: "n"
    ;RE-ALL
)#

Detailed description of regular expressions' syntax can be found here.

See also Notes.


Note 1: When working with regular expressions, use variable $0 to access the entire sequence, and variables $1 - $15 to access "remembered" matching groups of characters: $1 - first matching group, $2 - second matching group etc.

Example:

#( test_regexp
NoActive 
Action:
    RE-MATCH: "Firstname Lastname" "/(.*) (.*)/"
    IF
        MSG: "$0 = %$0%" \ $0 = Firstname Lastname
        MSG: "$1 = %$1%" \ $1 = Firstname
        MSG: "$2 = %$2%" \ $2 = Lastname
    THEN
)#

There is one another interesting regular expression usage example. Let's select all the *.wav files from directory c:\temp and change their file name extension from *.wav to *.mp3:

#( test_regexp_rename 
NoActive
Action: 
    FOR-FILES: "C:\TEMP\*.*" 
    RE-MATCH: "%FOUND-FULLPATH%" "/(.*)\.wav/i" 
    IF 
        \ $1 = full path and file name without file name extension
        FILE-RENAME: "%FOUND-FULLPATH%" "%$1%.mp3" 
    THEN 
;FOR-FILES 
)#

Note 2: Besides being used in words RE-MATCH: è RE-ALL:, regular expressions are also used in nnCron for creating masks for words which perform operations with window names.