StrExtract()

StrExtract() was added in VFP 7, probably to help with parsing XML statements, though it's really useful for parsing other kinds of strings, too (GenMenu and the FoxCode app that makes IntelliSense work both use it quite a bit). StrExtract() searches for a pair of delimiters, then returns the text between those delimiters. This is a brute force way to slice-and-dice a string; StrExtract does not honor the Document Object Model (DOM). For example, if you have a nested XML document where the customers and the orders both have a <STATE> attribute, StrExtract can't differentiate between the two. It also doesn't support the attribute-based <Customer State="MA"> </Customer> formats.

Usage

cText = STREXTRACT( cSearchIn , cStartDelimiter 
        [ , cEndDelimiter [ , nOccurrence [ , nFlag ]]]])

Parameter

Value

Meaning

cSearchIn

Character

The character string containing the text to search.

cStartDelimiter

Character

The text indicating the starting delimiter.

cEndDelimiter

Character

The text indicating the ending delimiter.

Omitted

No ending delimiter is specified; returns from the first delimiter to the end of the string.

nOccurrence

Numeric

Specifies the occurrence of the delimiters between which the text is returned (the first, second, nth, and so on).

Omitted

The first occurrence of the text is returned.

nFlag

1

Indicates that the search is not case-sensitive.

2

Indicates that the text specified in cEndDelimiter does not have to be found.

3

Indicates that the search is not case-sensitive, and that the cEndDelimiter text does not have to be found.

Omitted

No change to behavior: Case-sensitive search, where the end delimiter must be found.

cText

Character

The text between the specified delimiters.

Empty string

No match was found.


This function makes parsing delimited strings a breeze. Give it the string to search, and the starting and ending delimiters, and it returns the first occurrence. If you want to look for multiple occurrences, pass a number as the fourth parameter to tell it which occurrence to find. No, you can't return all the strings at once; see the example code for an example of parsing an XML string.Without the fifth parameter, StrExtract() looks for both the start and end delimiters, and if one or both are not found, the empty string is returned. However, you may want to eliminate looking for the ending delimiter. For example, in a comma-separated string, the last item in the list doesn't have a comma (or any other character) at the end, and passing 2 allows StrExtract() to return the last item. Omitting the parameter means the search is case-sensitive and both the start and end delimiters must be found.

Example

* A simple example that ALINES() could handle just as easily:
cList = "1, 2, 3, 4, 5"
? StrExtract(cList, ",", ",")  && Returns " 2"
 
* Parse an XML string.
cMyXML = "<Data>" + ;
         "<CLASS>" + ;
         " <NAME>Introduction to Developing with VFP</NAME>" + ;
         " <NUMBER>7101</NUMBER>" + ;
         "</CLASS>" + ;
         "<CLASS>" + ;
         " <NAME>Developing with VFP</NAME>" + ;
         " <NUMBER>7201</NUMBER>" + ;
         "</CLASS>" + ;
         "</Data>"
 
nTagCount = OCCURS("<NAME>", cMyString)
FOR nOccurrence = 1 TO nTagCount
  ? STREXTRACT(cMyString, "<NAME>", "</NAME>", nOccurrence)
ENDFOR

See Also

ALines(), At(), AtC(), CursorToXML(), GetWordCount(), GetWordNum(), Occurs(), FileToStr(), Rat(), RatC(), StrToFile(), StrTran(), SubStr(), SubStrC(), XMLToCursor()


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .