Extract$ extracts a substring based on left and right matches
; simple example: Let esi=
'This is a link to <a href="
http://www.google.com">Google</a> that can be extracted
' Print "The URL for ",
Extract$(esi,
Chr$(34, 62), "</a>"), " is "
; 34, 62 = "> Inkey Extract$(esi, "http://",
Chr$(34),
xsIncL)
; you could use '"' (single/double/single quote) instead of Chr$(34) ; result: The URL for Google is
http://www.google.com ; syntax:
Extract$(pSrc, "left match" [, "right match"] [,
xsFlags] [, maxlines
*)] [, startIndex])
; right match: if omitted, end of line is assumed
;
xsFlags: if omitted, search for left match is case-sensitive, left and right matches are excluded
; maxlines: default is 1, i.e. right match must be in same line; for extracting structures etc, put a reasonable value, e.g. 100
; startIndex: default is 1, i.e. beginning of string; if
xsLoop is set, search for the left match restarts where the last right match
was found; see also the options for
Instr_ -
Extract$ uses
Instr_ for the left match
xsCaseI ;
case-
insensitive search for left match (right: always case-sensitive)
xsIs ;
intelli
sense; search is case-insensitive for 1st char only, i.e.
Hello =
hello
xsI1c ;
ignore
1st
char in left match, e.g.
?:\Masm32\...
xsFullW ; full word (left match only)
xsIncL ; include left pattern (e.g.
http://)
xsIncR ; include right pattern
xsExcL ; exclude left pattern, e.g.
{url= ...
} xsExcR ; exclude right pattern
xsTrimL ; trim left side, i.e. strip everything <=ASCII 39 aka single quote
xsTrimR ; trim right side (after excluding right match if xsExcL is set)
xsTrim=xsTrimL or xsTrimR ; trim both sides
xsLineL ; include line of the left match
xsLineR ; include rest of line after the right match (must include right match...)
xsLoop ; let
Instr_ start behind the last position, for using in loops
*) if the right pattern contains a linefeed (LF, Ascii 10), the maxlines counter will never stop the pattern search
---------------------------------------------------------- The last flag,
xsLoop, is used in the following demo, a Windows console application that extracts
all structures from the two main Masm32 include files. Do
not use the result for work, as there are
problems with nested structures (e.g. unions ending with
ends) and some structures ending with
lowercase
ends.
include \masm32\MasmBasic\
MasmBasic.inc ;
download Init ; First, let's get some useful source string:
Let ecx=
FileRead$("\Masm32\include\Windows.inc")+
FileRead$("\Masm32\include\WinExtra.inc")
Open "O",
#1, "Structures.txt"
; open a file for output xor ebx, ebx
; reset counter .While 1
inc ebx
.Break .if
!Extract$(ecx, "STRUCT", 'ENDS',
xsFullW or xsLineL or xsIncR or xsLoop, 100)
Print #1, eax, CrLf$
.Endw
Close #1
; file #1 closed
Inkey Str$("%i structures found\n", ebx)
Exitend start
Rem -
Extract$ returns pointer in eax, and len of result in edx
- can be used with
Print and
Let even if no match found, i.e. eax=0; in this case,
Extract$ will print as
?RichMasm Key
ex$