The FindText method is used to search the
current message part for text which matches a specific pattern. This
method can only be used to search for text content in the current
message part. It cannot be used to search encoded data for file
attachments or other parts of a multipart message. It is intended to
only be used to search parts of the message which would potentially be
displayed to a user. The pattern you are matching against must be a
null-terminated string and it cannot consist of only whitespace
characters (spaces, tabs, carriage returns and linefeeds). This is
because a search for whitespace in an email message is not useful in
any practical sense, it would always match.
If the lpszPattern parameter does not contain any
regular expression constructs (such as ^, $,
[ ], or quantifiers like * and +),
the search is performed using a simple substring comparison. If a
regular expression pattern is specified, the method compiles the
pattern and searches the message content for a match. The regular
expression matching uses a simplified version which only supports
anchors, wildcards, match classes (enclosed in brackets) and a
limited number of escape sequences such as \d to match
any digit or \s to match any space character. It does
not support grouping or replacement.
Supported regular expression constructs include:
- The caret ^ and dollar symbol $ for the beginning and end of
the text. This would rarely be used in the context of email messages.
- The period . to match any character.
- Character classes such as [abc] which would match any of the
characters enclosed in brackets.
- Negated classes such as [^abc] which would match if none of
those characters are found.
- Escaped matches such as \d for digits, \w for word characters
and \s for whitespace characters
- The + quantifier can be used to match one or more occurrences.
- The * quantifier will match zero or more occurrences.
This method uses a minimal regular expression parser and does not
include support for complex constructs such as grouping matches. If
you want to search for text which may contain characters typically
used in regular expressions such as [ or $,
you can either escape them using a backslash, or you can use the
MIME_SEARCH_LITERAL_MATCH option which forces the method to only
perform a substring search.
Applications searching for arbitrary user-entered text should
usually specify the MIME_SEARCH_LITERAL_MATCH option to avoid
unintended interpretation of regular expression characters. This will
avoid situations where a user may unexpectedly enter search text which
appears to be a regular expression pattern.
If you provide a regular expression as the pattern to match, it is
also important to keep in mind that matches can span multiple lines
(they do not end at line breaks) and the regular expression algorithm
will attempt to match as much text as possible (this is sometimes
referred to as a "greedy match"). This can yield unexpected
results if you use wildcards in the pattern which are overly broad,
matching large sections of the message text. For example, a pattern
like ".*abc" will match everything until the last instance
of "abc" is found in the text.
Important: The optional lpnOffset and lpnLength
parameters can be used to provide more information about the location
and length of the pattern match in the message. However, there are
several important considerations with regards to these values. The
pattern search is performed after the message contents have been
decoded and/or converted from its original character set to UTF-8.
Because all message text is normalized as UTF-8 internally, regardless
of the original character set used to compose the message, this means
you should not attempt to search for text using non-ASCII characters
in your current locale. If the text was encoded, for example using
quoted-printable or base64 encoding, the offset and length values will
not be meaningful with regards to the original message content. The
GetText method will return a copy of the decoded
text.
If you specify the MIME_SEARCH_FILTER_HTML option, this will cause
the method to filter HTML markup from the message text prior to
matching the pattern against the contents. This is functionally
similar to calling the FilterText method with its
default options and then searching the plain text it returns. In this
case, the offset and length values are only relevant to the filtered
text, not the original text in the message. If you want to manipulate
the filtered text after performing a search, for consistency you
should always use FilterText to obtain the filtered text
rather than GetText.
If you want to return a list of links in the current message, a
simpler approach is to use the GetLinks method
which designed specifically for this purpose.