|
Article
|
Difficulty
Rating
|
5
|
Finding
and Replacing
Text
|
|
Metacard
offers flexible and
effective features to
search and replace text.
There are various
methods to go about
this, from relatively
straightforward commands
like find and replace,
to more advanced
functions like
matchText()
and
replaceText().
This article focuses on
the more straightforward
searching features. For
more information about
advanced features see
the article on
text
management.
The Find
Command
One
way to search for a text
string is to use the
find
command. It is what the
MetaCard Find tool uses,
and has a number of
advantages. Firstly, it
can be used to search
multiple fields in the
defaultStack,
as well as specific
fields.
|
find "Joe" -- searches all the fields in the stack card by card
find "Joe" in field "Name" -- searches the "Name" field in all the cards
Secondly,
you can control
characteristics of the
search by specifying the
<type>, in the
form:
find <type> <expression>
<Type>
can be
chars,
string, whole,
or
word.
Using
chars
finds exact character
matches anywhere in
words, but does not
match text expressions
longer than a single
word.
String
finds exact character
matches anywhere in
words, and includes
spaces and multiple
words.
Whole
only matches the entire
expression if it is
complete words, and
word
matches whole words but
only matches the first
word in the expression.
The
following table gives
examples of
find
commands and the text
which is matched using
different search
characteristics.The
first line in the table
is equivalent to
typing:
find chars "man"
Examples
of the Find Command,
operating in a field
containing the words
"The yellow
man".
|
Type
|
Search
|
the
result
|
the
foundText
|
chars
|
man
|
empty
|
man
|
string
|
man
|
empty
|
man
|
whole
|
man
|
empty
|
man
|
word
|
man
|
empty
|
man
|
|
|
|
|
chars
|
e
yellow
|
empty
|
e
|
string
|
e
yellow
|
empty
|
e
yellow
|
whole
|
e
yellow
|
not
found
|
|
word
|
e
yellow
|
not
found
|
|
|
|
|
|
chars
|
yellow
man
|
empty
|
yellow
|
string
|
yellow
man
|
empty
|
yellow
man
|
whole
|
yellow
man
|
empty
|
yellow
man
|
word
|
yellow
man
|
empty
|
yellow
|
|
|
|
|
chars
|
man
yellow
|
empty
|
man
|
string
|
man
yellow
|
not
found
|
|
whole
|
man
yellow
|
not
found
|
|
word
|
man
yellow
|
empty
|
man
|
There
are a number of
functions which give
information about the
text which has been
found with the
find
command. The
foundChunk
returns a chunk
expression describing
where the text was
found, e.g. char 2 to 3
of field 1.
The
foundText
(used in the field
above) contains the
actual text found. The
foundLine
returns the number of
the line the text was
on. The
foundField
returns the field name.
The
foundLoc
returns the coordinates
of the border drawn
around the text that was
found.
If
you do not want certain
fields such as labels to
be included in a general
search, set their
dontSearch
property to true. If you
want to discriminate
capital letters and
lowercase letters set
the
caseSensitive
local property to true.
This property also
applies to other methods
of searching besides the
find
command, such as the
offset
functions.
|
TIP:
If you want to use the
find
command, but you want
the text which is found
to be selected as a
normal text selection,
rather than the standard
black border, use the
following
script:
lock screen -- avoids any screen flicker
find <expression>
select the foundChunk
unlock screen
|
The Offset
Function
Despite
the flexibility of the
find
command, it is not
always the best feature
for
searching.
The
offset
function is a simple
alterative to
find.
It is generally faster
than the
find
command, and has the
advantage that it can be
used in other containers
besides fields, such as
variables. It also does
not display any results,
leaving you free to
generate user feedback
to the search in any way
you like.
Offset
returns the character
number of a specified
string within a whole
string.
|
put offset("there", "hello there")
-- returns 7 since the t in "there" is character 7 in "hello there"
If
the specified string is
not within the whole
string 0 is returned. To
use the
offset
function like the
find
command, try something
similar to the following
script:
|
ask
"Enter the text to
find?"
if the result is not "Cancel" then \
select char offset(it, field 1) \
to offset(it, field 1) + length(it) - 1 of field 1
-- select from the first character to the last character to be found
-- the length function returns the number of characters in a string
-- the \ character is simply used to separate long lines
The
offset
functions returns
character offsets, but
there are a set of other
similar functions that
work in relation to
different chunks:
itemOffset,
wordOffset,
lineOffset.
A useful property to
remember when using
these is the
wholeMatches
property. When set to
true it forces all
matches made to be
complete chunks, i.e.
complete items, words,
or lines. When false, it
allows partial
matches.
Useful
Operators
There
are various operators
which can be useful
during text searches.
The
contains,
is
in
and
is
among
operators can be used to
make text comparisons.
Of these, only is among
merits further
explanition. It will
only return true when a
complete match is
made.
if field 1 contains "hello" then beep
if "ello" is in "hello" then beep
if "hello" is among the items of fld 1 then beep
if "This complete line." is among the lines of fld "example" then beep
The Replace
Command
The
replace command can be
used to replace text
strings in containers.
It replaces all
instances of a text
string in the specified
container. For more
complex replacements,
see the article on
text
management.
The syntax is as
follows:
|
replace <text> with <replacement text> in <container>
For
example:
replace "his" with "her" in field 1
replace "this" with "that" in MyVariable
Did you find this
article useful? Have any
ideas for future topics?
Email
Us!
|
|