PBXB64 String Operations
30+ Functions
Standard
PBXB64 V18 combines 30+ built-in string functions with EVAL$, StringX comparisons, wildcard matching, searching, replacing, and REGEX helpers. ANSI and Unicode operations use the same intrinsics: LEN, LEFT$, RIGHT$, MID$, MID$ assignment, INSTR, UCASE$, LCASE$, TRIM$, LSET, and RSET work with both STRING and WSTRING.
Built in - no external regex DLL
REGEXPR 1.25 Regular Expressions
PBXB64 V18 exposes the integrated REGEXPR 1.25 engine through PB-compatible statements and StringX helpers. It supports groups and backreferences, bounded quantifiers, alternation, word boundaries, character classes, greedy and shortest matching, case control, binary-safe target strings, match positions, captured text, and global replacement.
Extract captured data
REGEX$ returns a selected capture group
FUNCTION PBMAIN() AS LONG
LOCAL source AS STRING
LOCAL pattern AS STRING
LOCAL nameOnly AS STRING
source = "User: Ada Lovelace, ID: 1843"
pattern = "User: ([A-Za-z]+ [A-Za-z]+)"
nameOnly = REGEX$(pattern, source, 1)
PRINT nameOnly ' Ada Lovelace
FUNCTION = 0
END FUNCTION
A single expression finds the match and returns capture group 1. No manual scanning, delimiter counting, or temporary parser state is needed.
Reformat every match
REGEXREPLACE$ with capture references
FUNCTION PBMAIN() AS LONG
LOCAL result AS STRING
result = REGEXREPLACE$("([A-Za-z]+) ([A-Za-z]+)", _
"Ada Lovelace and Grace Hopper", "$2, $1")
PRINT result ' Lovelace, Ada and Hopper, Grace
FUNCTION = 0
END FUNCTION
$0, $1, and ${1}-style references make structured replacement concise. The replacement runs over all matches.
Get position and length
PowerBASIC-compatible REGEXPR statement
LOCAL source AS STRING, startAt AS LONG, matchLen AS LONG
source = "Order AB-2048 is ready"
REGEXPR "[A-Z]{2}-[0-9]{4}" IN source TO startAt, matchLen
PRINT startAt, matchLen
The statement form returns 1-based position and length, so existing PB-style code can continue with MID$ or use the offsets in a parser.
PB-dialect replacement
REGREPL statement with backreferences
LOCAL source AS STRING, result AS STRING, nextAt AS LONG
source = "John Smith"
REGREPL "([a-z]+) ([a-z]+)" IN source _
WITH "\02, \01" TO nextAt, result
PRINT result ' Smith, John
The statement syntax supports PB-style \00 through \99 replacement tags and reports the next position together with the result.
ANSI String Functions
ANSI string operations
FUNCTION PBMAIN() AS LONG
LOCAL s AS STRING
LOCAL t AS STRING
s = " Hello World "
PRINT "LEN: "; LEN(s) ' 15
PRINT "TRIM$: "; TRIM$(s) ' "Hello World"
PRINT "LCASE$: "; LCASE$(s) ' " hello world "
PRINT "UCASE$: "; UCASE$(s) ' " HELLO WORLD "
t = LEFT$(s, 7) ' " Hello"
t = RIGHT$(s, 6) ' "orld "
t = MID$(s, 5, 3) ' "lo "
PRINT "INSTR: "; INSTR(s, "World") ' 9
' Assignment via MID$
MID$(s, 9, 5) = "Universe"
PRINT "After: "; s ' " Hello Universe"
FUNCTION = 0
END FUNCTION
Full ANSI string API: LEN, LEFT$, RIGHT$, MID$, INSTR, UCASE$, LCASE$, TRIM$, LTRIM$, RTRIM$, SPACE$, STRING$, CHOOSE$, FORMAT$, STR$, VAL, HEX$, OCT$, BIN$.
WSTRING - Wide Character Strings
Full Unicode support with no special commands: LEN, LEFT$, RIGHT$, MID$, INSTR, UCASE$, LCASE$, and TRIM$ work directly on WSTRING. UTF-16 encoding, correct length counting, and BYREF passing.
WSTRING Unicode operations
FUNCTION PBMAIN() AS LONG
LOCAL ws AS WSTRING
ws = "Hello World"
PRINT "LEN: "; LEN(ws) ' 11
PRINT "LEFT$: "; LEFT$(ws, 5) ' "Hello"
PRINT "RIGHT$: "; RIGHT$(ws, 5) ' "World"
PRINT "MID$: "; MID$(ws, 7, 3) ' "Wor"
' Unicode content
ws = "Grüße"
PRINT "LEN: "; LEN(ws) ' 5 (characters, not bytes)
FUNCTION = 0
END FUNCTION
No separate W-commands needed: LEN, LEFT$, RIGHT$, MID$, INSTR, UCASE$, LCASE$, and TRIM$ detect WSTRING automatically. Correct character counting (not byte counting) for UTF-16 content.
LSET / RSET - Fixed-Width Padding
Left/right padding
FUNCTION PBMAIN() AS LONG
LOCAL s AS STRING
s = SPACE$(20)
LSET s = "Hello" ' Left-aligned, padded right
PRINT "LSET: |"; s; "|"
s = SPACE$(20)
RSET s = "Hello" ' Right-aligned, padded left
PRINT "RSET: |"; s; "|"
FUNCTION = 0
END FUNCTION
LSET and RSET are real implementations (not stubs), producing left-aligned and right-aligned fixed-width strings. Also: CSET/CSET$ for centering.
BYREF String Parameters
Strings passed BYREF are correctly handled with caller-owned lifetime and GEP-based addressing.
BYREF string modification
FUNCTION ModifyString(BYREF s AS STRING) AS LONG
s = "Modified"
FUNCTION = 0
END FUNCTION
FUNCTION PBMAIN() AS LONG
LOCAL msg AS STRING
msg = "Original"
ModifyString(msg)
PRINT msg ' "Modified"
FUNCTION = 0
END FUNCTION
BYREF string parameters pass the address (not the value), allowing the callee to modify the caller's string. String cleanup is caller-owned, preventing double-free.