Search and Translate Primitives

Summary
Search and Translate Primitives
Macros
quote_regexpQuote regexp special characters.
re_compCompile a regular expression.
re_deleteDelete a compiled expression.
re_resultRetrieve search captures.
re_searchSearch for a string.
re_syntaxSet the regular expression mode.
re_translateSearch and replace.
search_backBackwards buffer search.
search_caseSet the search pattern case mode.
search_fwdBuffer search.
translateBuffer search and replace.

Macros

quote_regexp

string quote_regexp(string text)

Quote regexp special characters.

Description

The quote_regexp() primitive quotes (i.e. prefixes with a backslash) the following set of predefined characters in the specified string text, representing most of the regular expression special characters.

The predefined characters are;

  • asterisk (*)
  • backslash (\)
  • caret (^)
  • curvy brackets ({})
  • dollar sign ($)
  • parenthesis (())
  • percentage (%)
  • period (.)
  • pipe (|)
  • plus sign (+)
  • question mark (?)
  • square brackets ([])

Parameters

textString containing the text to quote, protecting special regular expression characters..

Returns

The quote_regexp() primitive returns the resulting quoted string.

Portability

n/a

See Also

search_fwd, re_search

re_comp

list re_comp( [int flags],
string pattern,
 [string &error])

Compile a regular expression.

Description

The re_comp() primitive is reserved for future development.

The re_comp primitive converts a regular expression string (RE) into an internal form suitable for pattern matching.

Parameters

flagsOptional integer flags, one or more of the SF_XXX flags OR’ed together control the options to be applied during the search and replace.
patternString containing the pattern to translate.
errorOptional string reference, on an error shall be populated with a description of the error.
parmsOptional string containing configuration parameters.

Returns

The re_comp() primitive returns list containing the compiled expression, otherwise a null list.

Warning:

DONOT modify the result otherwise you shall encounter undefined results during search operations.

Portability

n/a

See Also

re_search, re_translate, re_delete

re_delete

int re_delete(list handle)

Delete a compiled expression.

Description

The re_delete() primitive

Parameters

handle -

Returns

The re_delete() primitive returns 1 on success, otherwise 0.

Portability

n/a

See Also

re_comp

re_result

int re_result([int capture],
 [string &value],
[int &offset],
[int &length])

Retrieve search captures.

Description

The re_result() primitive retrieves the results of the last regular expression re_search operation.  In addition the prior search must have had SF_CAPTURES enabled.

Warning:

The captured regions reference the original searched object, as such the original buffer, list or string *must not be modified prior to executing re_result.

Parameters

captureInteger capture index, see details below.
valueString variable reference, is populated with the value of the referenced capture.
offsetOptional integer variable reference, is populated with the starting offset of the capture within the original source.
Capture Index
Value
Description
1 ..  9
Xth captured expression.
CAPTURE_BEFORE
Everything prior to matched string
CAPTURE_ARENA
Entire matched string.
CAPTURE_AFTER
Everything after to matched string
CAPTURE_LAST
Last parenthesized pattern match.

Returns

The re_result() primitive returns the length of the capture, otherwise -1 on error.

Portability

A GriefEdit extension.

See Also

re_search, search_fwd, search_back, search_string, search_list

re_search

int re_search([int flags],
[string pattern],
 [declare object],
[int start],
[int lensym])

Search for a string.

Description

The re_search() primitive is a generalised search interface, combining the functionality of the more specialised search primitives but presenting consistent and stateless search capabilities.

In addition both the re and case modes are explicit based upon the flags values.

Parameters

flagsOptional integer flags, one or more of the SF_XXX flags OR’ed together control the options to be applied during the search.
patternA string containing the pattern to be matched.
objectOptional object to be searched.  The search depends on the type of object.  If object is a string, then a string search is performed.  If object is a list then a list search is done.  Ifn an integer the buffer associated with the stated buffer number is searched, otherwise if omitted a search on the current buffer is performed.
startOptional integer index for list objects, states the element offset at which search operation shall start.  If omitted, the search is started from the beginning of the list.
lensymOptional integer reference for string objects is populated with the length of the matched region.
Flags

General control flags.

Constant
Description
SF_BACKWARDS
Search in backwards direction.  By default searchs are performed in a forward direction; only meaningful for buffer searches or translates.
SF_IGNORE_CASE
Ignore/fold case.
SF_BLOCK
Restrict search to current block.
SF_LINE
Line mode.
SF_LENGTH
Return length of match.
SF_MAXIMAL
BRIEF maximal search mode.
SF_CAPTURES
Capture elements are retained.
SF_QUIET
Do not display progress messages.
SF_GLOBAL
Global translate (*); re_translate only.
SF_PROMPT
Prompt for translate changes; re_translate only.
SF_AWK
awk(1) style capture references.
SF_PERLVARS
perl(1) style capture references.

(*) PROMPT is given priority over GLOBAL.

Syntax selection, one of the following mutually exclusive options can be stated.

Constant
Description
SF_NOT_REGEXP
Treat as plain test, not as a regular expression.
SF_BRIEF/SF_CRISP
BRIEF/CRiSP/GRIEF expressions (default).
SF_UNIX
Unix regular expressions.
SF_EXTENDED
Extended Unix expressions.
SF_PERL
PERL syntax.
SF_RUBY
Ruby syntax.

Returns

The re_search() primitive return is dependent on the type of the object searched; matching the corresponding specialised primitive search_fwd, search_list and search_string.  These are summarised below.

Object Type
return
buffer
on success the length of the matched text; otherwise 0 if no match or -1 on error.
list
on success index of the matched element, otherwise -1 if on match.
string
on success the index of first character of match, otherwise 0 if no match.

On error re_search returns -1.

Portability

n/a

See Also

re_translate, search_fwd, search_back, search_string, search_list, re_syntax

re_syntax

int re_syntax([int re],
[int case],
[int capture])

Set the regular expression mode.

Description

The re_syntax() primitive configures the global regular expression syntax mode to the mode re.  By default, the mode is set to the RE_BRIEF regular expression syntax.

re_syntax affects the defaults of the following primitives;

The supported regular expression syntax modes.

Value
Constant
Description
0
RE_BRIEF
BRIEF/Crisp Edit mode.
1
RE_UNIX
Basic Unix syntax.
2
RE_EXTENDED
POSIX Extended Unix.
3
RE_PERL
Perl.
4
RE_RUBY
Ruby style syntax.

Parameters

reOptional integer value, specifying the new regular expression syntax to by applied.  If omitted, the current value is unchanged.
caseOptional integer value, specifying the new state of the global case mode.  If case is specified and is zero, then searches are performed as case insensitive, otherwise when non-zero they shall be case sensitive.  If omitted, the current value is unchanged.
captureOptional integer value, specifying the new state of the global replacement pattern reference mode.  If RE_PERLVARS then Perl style capture references shall be utilised, when RE_AWKVARS AWK style capture references are utilised, otherwise then 0 the style is dependent on the re mode; see translate.  If omitted, the current value is unchanged.

Returns

The re_syntax() primitive returns the current/resulting regular expression syntax mode.

Portability

The case and re arguments are GriefEdit extensions.

See Also

search_case, re_search

re_translate

int|string re_translate( [int flags],
string pattern,
  [string replacement],
 [declare object])

Search and replace.

Description

The re_translate() primitive is a generalised search and replace interface, combining the functionality of the more specialised primitives but presenting consistent and stateless search capabilities.

Parameters

flagsOptional integer flags, one or more of the SF_XXX flags OR’ed together control the options to be applied during the search and replace.
patternString containing the pattern to translate.
replacementString containing the replacement expression for each matched pattern.  It may contain references to the pattern’s capture groups by special variables using one of three forms GRIEF, AWK or Perl, see below.
objectOptional object to be searched.  The search depends on the type of object.  If object is a string, then a string search is performed; translating a string provides similar functionality to awk’s sub() and gsub() functions.  If object is a list then a list search is done.  If an integer the buffer associated with the stated buffer number is searched, otherwise if omitted a search on the current buffer is performed.
Special Variables
  • GRIEF variables - GRIEF style references.

This style take the form of of the form “\d”, where d is a numeric group number started at an index of 0; note escapes need to be preceded by an additional backslash.

Variable
Value
\0, \1 ... 
Xth + 1 captured expression.
  • AWK Variable - AWK style references are selected using SF_AWK.

This style take the form of “\d”, where d is a numeric group number indexed from 1; note escapes need to be preceded by an additional backslash.

Variable
Value
\0
Matched string.
\1, \2 ... 
Xth captured expression.
&
Match string, same as \0.
  • PERL Variables - PERL style references are selected using SF_PERLVARS; is it also implied under SF_PERL, SF_RUBY and SF_TRE unless SF_AWK is stated.

This style is super-set of all three forms.  Capture references take the form “$x” when x is a numeric group number indexed from 1 or one of the special variables $`, $&, and $’.  In addition the AWK “\x” syntax is supported, yet their use should be avoided due to the need to quote the escapes.

Variable
Value
\1, \2 ... 
Old style, Xth captured expression.
$1, $2 ... 
Xth captured expression.
${xx}
Xth captured expression.
$+
Last parenthesized pattern match.
$`
Everything prior to the matched string.
$&
Entire matched string.
$’
Everything after the matched string.

Returns

If object is a string, then the resulting translated string is returned.

Otherwise the re_translate() primitive returns the number of translations which occurred, otherwise -1 on error.

Portability

n/a

See Also

re_search, translate

search_back

int search_back(string pattern,
 [int re],
 [int case],
  [int block],
 [int length])

Backwards buffer search.

Description

The search_back() primitive searches backwards within the active buffer from the current cursor position to the buffer top (or block) against the expression pattern.  The treatment of the matched pattern may either be a regular-expression or constant string dependent on re and case folding based on case.

Note:

The search_back primitive is provided primary for BRIEF compatibility.  Since re and case can reference the current global search states, inconsistent and/or unexpected results may result between usage; as such it is advised that the state-less re_search primitive is used by new macros.

Parameters

patternA string containing the pattern to be matched.
reOptional integer value.  If re is specified and is zero, then pattern is treated as a literal string not as a regular expression; behaviour being the same as index.  Otherwise the string shall be treated as a regular expression using the current global syntax, see re_syntax and search_back for additional information.
caseOptional integer value specifying whether case folding should occur.  If case is specified and is zero, then the search is done with case insensitive.  If omitted the current global setting is referenced.
blockOptional integer flag.  If block is specified and is non-zero, the search operations are limited to the current marked region.
lengthOptional inter flag.  If length is specified and is non-zero, indicates that the total length of the text should be returned; regardless of any \\c anchors in the pattern.

Returns

The search_back() primitive returns the length of the matched text plus one otherwise zero or less if no match was found.

Alternatively, if length is specified the total length of the matched text is returned.  Otherwise if a \\c anchor was encountered within the pattern, it returns the length of the text from the position of the marker to the end of the matched text plus one.

Portability

n/a

See Also

search_fwd

search_case

int search_case([int case])

Set the search pattern case mode.

Description

The search_case() primitive sets or toggles the global search case mode, if omitted the current mode is toggled.

On completion the resulting mode is echo on the command prompt, either as

Case sensitivity on

or

Case sensitivity off

By default all searches are case sensitive, that is A and a are not equivalent, by setting the case mode to a zero value, case sensitivity will be ignored when performing matches.

search_case affects the default case sensitivity of the follow primitives.

Parameters

caseOptional integer value, specifying the new state of the case mode.  If case is omitted, the current value is toggled.

Returns

The search_case() primitive returns the previous value of the case mode.

Portability

n/a

See Also

search_back, search_fwd, translate, re_syntax

search_fwd

int search_fwd(string pattern,
 [int re],
 [int case],
  [int block],
 [int length])

Buffer search.

Description

The search_fwd() primitive searches forward within the active buffer from the current cursor position to the buffer end (or block) against the expression pattern.  The treatment of the matched pattern may either be a regular-expression or constant string dependent on re and case folding based on case.

Note:

The search_fwd primitive is provided primary for BRIEF compatibility.  Since re and case can reference the current global search states, inconsistent and/or unexpected results may result between usage; as such it is advised that the state-less re_search primitive is used by new macros.

Parameters

patternA string containing the pattern to be matched.
reOptional integer value.  If re is specified and is zero, then pattern is treated as a literal string not as a regular expression; behaviour being the same as index.  Otherwise the string shall be treated as a regular expression using the current global syntax, see re_syntax and search_back for additional information.
caseOptional integer value specifying whether case folding should occur.  If case is specified and is zero, then the search is done with case insensitive.  If omitted the current global setting is referenced.
blockOptional integer flag.  If block is specified and is non-zero, the search operations are limited to the current marked region.
lengthOptional inter flag.  If length is specified and is non-zero, indicates that the total length of the text should be returned; regardless of any \\c anchors in the pattern.

Returns

The search_fwd() primitive returns the length of the matched text plus one otherwise zero or less if no match was found.

Alternatively, if length is specified the total length of the matched text is returned.  Otherwise if a \\c anchor was encountered within the pattern, it returns the length of the text from the position of the marker to the end of the matched text plus one.

Portability

n/a

See Also

search_back

translate

int translate(string pattern,
string replacement,
  [int global],
 [int re],
 [int case],
  [int block],
 [int forward])

Buffer search and replace.

Description

The translate() primitive perform string translations within the current buffer.  The translate starting at the current cursor position, continuing until the end of the buffer or optionally terminated by the end user.

If either the pattern or replacement is not specified, then the user is prompted for the change.

Unless global is specified, during translate operations the user to informed on the first match and prompted for the desired action.

Change [Yes|No|Global|One|Entire|Abort,Top|Center]?

the actions being

YesReplace current match with the pattern and continue searching.
NoDo not replace current match with the pattern and continue searching.
GlobalReplace all matches from cursor to end of file.
EntireReplace all matches in the file.
AbortStop the translation.
TopMoves cursor and the associated text for better visibility to top of page and re-prompts.
CenterCenters the cursor and the associated text for better visibility and re-prompts.

Note:

The translate primitive is provided primary for BRIEF compatibility.  As the values of re and case can reference the current global state resulting in possible inconsistent results between usage, it is advised that the state-less re_translate primitive is used by new macros.

Parameters

patternString containing the pattern to translate.
replacementString containing the replacement expression for each matched pattern.  It may contain references to the pattern’s capture groups by special variables using one of three forms GRIEF, AWK or Perl, see below.
globalOptional integer flag controls user prompts.  If specified as non-zero every occurence of the pattern is replaced, otherwise only the first occurance is translated.  If global is omitted the user is prompted on each match.
reOptional integer value.  If re is specified and is zero, then pattern is treated as a literal string not as a regular expression; behaviour being the same as index.  Otherwise the string shall be treated as a regular expression using the current global syntax, see re_syntax and search_back for additional information.
caseOptional integer value specifying whether case folding should occur.  If case is specified and is zero, then the search is done with case insensitive.  If omitted the current global setting is referenced.
blockOptional integer flag.  If block is specified and is non-zero, the search operations are limited to the current marked region.
forwardOptional integer flag specifying the search direction.  If forward is specified as non-zero or omitted, the translation operation moves forward.  Otherwise the translation moves backwards until the top of buffer.

Returns

The translate() primitive returns the number of translations which occurred, otherwise -1 on error.

Portability

n/a

See Also

search_fwd, search_back, re_search

$Id: $

To send feedback on this topic email: grie.nosp@m.fedit@gmai.nosp@m.l.com

Copyright © Adam Young All Rights Reserved.

string quote_regexp(string text)
Quote regexp special characters.
list re_comp( [int flags],
string pattern,
 [string &error])
Compile a regular expression.
int re_delete(list handle)
Delete a compiled expression.
int re_result([int capture],
 [string &value],
[int &offset],
[int &length])
Retrieve search captures.
int re_search([int flags],
[string pattern],
 [declare object],
[int start],
[int lensym])
Search for a string.
int re_syntax([int re],
[int case],
[int capture])
Set the regular expression mode.
int|string re_translate( [int flags],
string pattern,
  [string replacement],
 [declare object])
Search and replace.
int search_back(string pattern,
 [int re],
 [int case],
  [int block],
 [int length])
Backwards buffer search.
int search_case([int case])
Set the search pattern case mode.
int search_fwd(string pattern,
 [int re],
 [int case],
  [int block],
 [int length])
Buffer search.
int translate(string pattern,
string replacement,
  [int global],
 [int re],
 [int case],
  [int block],
 [int forward])
Buffer search and replace.
int search_string(string pattern,
string text,
  [int &length],
 [int re],
 [int case])
Searches for a pattern in a string.
int search_list( [int start],
string pattern,
 list expr,
 [int re],
 [int case])
Search list contents.
int index(string str,
int ch|string s)
Search string for a leftmost sub-string or character.