'' EXTRACT (LIKE +) Code Challenge - Script BASIC / JRS
html = """<!DOCTYPE html>
<html>
<head>
<title>AllBASIC.INFO Forum LIKE Code Challenge</title>
</head>
<body>
LIKE it or don't.
</body>
</html>"""
FUNCTION EXTRACT(base, mask, select)
UNDEF MATCH
start = 1
match_idx = 2
SPLITA mask BY "*" TO patterns
FOR idx = 0 TO UBOUND(patterns) STEP 2
MATCH[match_idx, 1] = INSTR(base, patterns[idx], start)
IF MATCH[match_idx, 1] <> undef THEN
start = MATCH[match_idx, 1]
ELSE
GOTO MATCH_ERROR
END IF
MATCH[match_idx, 2] = INSTR(base, patterns[idx + 1], start)
IF MATCH[match_idx, 2] <> undef THEN
start = MATCH[match_idx, 2]
ELSE
GOTO MATCH_ERROR
END IF
MATCH[match_idx, 1] = MATCH[match_idx, 1] + LEN(patterns[idx])
MATCH[match_idx, 2] = MATCH[match_idx, 2] - MATCH[match_idx, 1]
MATCH[match_idx, 0] = MID(base, MATCH[match_idx, 1], MATCH[match_idx, 2])
match_idx += 2
NEXT
IF select <> undef AND select <= UBOUND(patterns) + 1 THEN
EXTRACT = MATCH[select, 0]
' UNDEF MATCH
ELSE
EXTRACT = UBOUND(patterns) + 2
END IF
GOTO DONE
MATCH_ERROR:
EXTRACT = 0
DONE:
END FUNCTION
PRINT EXTRACT(html, "*<title>*</title>*<body>*</body>*", 2), "\n"
PRINTNL
PRINT "Title: ", MATCH[2, 0],"\n"
PRINT "Start: ", MATCH[2, 1],"\n"
PRINT "Length: ", MATCH[2, 2],"\n"
PRINTNL
PRINT "Body: ", MATCH[4, 0],"\n"
PRINT "Start: ", MATCH[4, 1],"\n"
PRINT "Length: ", MATCH[4, 2],"\n"