Xbasic

EXTRACT_STRING Function

Syntax

Substring as C = extract_string(string as C,start_string as C,end_string as C [,occurrence as N [,include_tags as L [,case_sensitive as L [,regex_safe as L]]]])

Arguments

extract_stringCharacter

The string extracted from Search_in_String. Character

stringCharacter

The string to examine for the find text.

start_stringCharacter

The beginning characters of the find text.

end_stringCharacter

The ending characters of the find text. Character

occurrenceNumeric

Default = 1. The nth occurrence to return. The occurrence parameter can be negative, in which case, Alpha Anywhere will return the nth occurrence from the end of the string.

include_tagsLogical

Default = .F.; .T. = Include the Start_Find and Start_Find characters.

.F. = Return only the text between the Start_Find and Start_Find characters.

case_sensitiveLogical

Default = .F. , .T. = Case sensitive. , .F. = Case insensitive.

regex_safeLogical

Default = .F. The "unsafe" characters are ., |, *, ?, +, (, ), {, }, , , ^, $ and \. If your Start_Find and End_Find do not contain any of these characters, you should set Regex_Safe to true to maximize performance.

.T. = Improves performance by declaring that the start and end tags are already regular expression safe and EXTRACT_STRING() does not need to escape special characters.

.F. = Unsafe characters are replaced. This decreases performance on a large string.

Returns

SubstringCharacter

Returns the substring that matches the search criteria.

Description

Extracts the nth occurrences of a sub-string starting with 'start_string' and ending with 'end_string'.

Discussion

The EXTRACT_STRING() function extracts a sub-string from a string. The sub-string is defined by a starting and ending block of text. For example:

Example

html = <<%txt%
<table>
    <tr><td>Row 1, Col 1</td><td>Row 1, Col 2</td><td>Row 1, Col 3</td></tr>
    <tr><td>Row 2, Col 1</td><td>Row 2, Col 2</td><td>Row 2, Col 3</td></tr>
</table>
%txt%
st = "<td>"
en = "</td>"
? extract_string(html, st, en, 1)
= "Row 1, Col 1"


? extract_string(html, st, en, 3, .t.)
= "<td>Row 1, Col 3</td>"


? extract_string(html, st, en, -1)
= "Row 2, Col 3"

See Also