RosettaCodeData/Task/Regular-expressions/BBC-BASIC/regular-expressions.basic

41 lines
1.3 KiB
Plaintext

SYS "LoadLibrary", "gnu_regex.dll" TO gnu_regex%
IF gnu_regex% = 0 ERROR 100, "Cannot load gnu_regex.dll"
SYS "GetProcAddress", gnu_regex%, "regcomp" TO regcomp
SYS "GetProcAddress", gnu_regex%, "regexec" TO regexec
DIM regmatch{start%, finish%}, buffer% 256
REM Find all 'words' in a string:
teststr$ = "I love PATTERN matching!"
pattern$ = "([a-zA-Z]+)"
SYS regcomp, buffer%, pattern$, 1 TO result%
IF result% ERROR 101, "Failed to compile regular expression"
first% = 1
REPEAT
SYS regexec, buffer%, MID$(teststr$, first%), 1, regmatch{}, 0 TO result%
IF result% = 0 THEN
s% = regmatch.start%
f% = regmatch.finish%
PRINT "<" MID$(teststr$, first%+s%, f%-s%) ">"
first% += f%
ENDIF
UNTIL result%
REM Replace 'PATTERN' with 'pattern':
teststr$ = "I love PATTERN matching!"
pattern$ = "(PATTERN)"
SYS regcomp, buffer%, pattern$, 1 TO result%
IF result% ERROR 101, "Failed to compile regular expression"
SYS regexec, buffer%, teststr$, 1, regmatch{}, 0 TO result%
IF result% = 0 THEN
s% = regmatch.start%
f% = regmatch.finish%
MID$(teststr$, s%+1, f%-s%) = "pattern"
PRINT teststr$
ENDIF
SYS "FreeLibrary", gnu_regex%