rexx-things/projects/oorexx/regex-test.rex
2025-03-21 22:27:26 +00:00

40 lines
1.5 KiB
Rexx

/*....v....1....v....2....v....3....v....4....v....5....v....6....v....7....v....8....v....9....v....0 */
/*drwx------ 2 gmgauthier gmgauthier 4.0K Feb 28 20:58 .a68g/ */
/*-rw-r--r-- 1 gmgauthier gmgauthier 3.1K Feb 23 22:41 .tmux.conf */
dir_str = "drwx------ 2 gmgauthier gmgauthier 4.0K Feb 28 20:58 .a68g/"
filestr = "-rw-r--r-- 1 gmgauthier gmgauthier 3.1K Feb 23 22:41 .tmux.conf"
/* attrs num o g sz mo dy tm filename */
dir_regex = .RegularExpression~new("?{10} ?{3} ?{10} ?{10} ?{4} [:alpha:]{3} [:digit:]{2} [:digit::punct:]{5} ([:xdigit::punct:]*)", "MINIMAL")
say "String to Match: " filestr
say "Match Result: " dir_regex~match(filestr)
say "Match Location: " dir_regex~Position
say "Match String: " filestr~substr(dir_regex~position+1)
say
nrs = 1, 42, 0, 5436412, "1A", "f43g"
re = .RegularExpression~new("[1-9][0-9]*")
do nr over nrs
say nr "is" re~match(nr)~?("a valid", "an invalid") "number"
end
say
-- allow hexadecimal numbers and a single 0
re~parse("0|([1-9a-fA-F][:xdigit:]*)")
do nr over nrs
say nr "is" re~match(nr)~?("a valid", "an invalid") "number"
end
say
text = "It's the year 2025!"
say text
re = .RegularExpression~new("[1-9][0-9]*")
begin = re~pos(text)
if begin > 0 then
do
year = text~substr(begin, re~position - begin + 1)
say "Found the number" year "in this sentence."
end
Exit
::requires "rxregexp.cls"