Added some standard C regex implementation. (0.20.0)
This commit is contained in:
parent
63edf97aba
commit
94602f0855
12 changed files with 438 additions and 1 deletions
38
examples/regex_demo.fun
Normal file
38
examples/regex_demo.fun
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
// Simple demo of Regex stdlib class.
|
||||
|
||||
include <regex.fun>
|
||||
|
||||
r = Regex()
|
||||
|
||||
text = "abc-123-xyz"
|
||||
|
||||
// full match (should be 0)
|
||||
m1 = r.match(text, "[a-z]+-[0-9]+-[a-z]+$")
|
||||
print(m1)
|
||||
|
||||
// search
|
||||
s1 = r.search(text, "[0-9]+")
|
||||
print(s1)
|
||||
|
||||
// replace digits with #
|
||||
out = r.replace(text, "[0-9]", "#")
|
||||
print(out)
|
||||
|
||||
/* Expected output:
|
||||
1
|
||||
{"match": 123, "start": 4, "end": 7, "groups": []}
|
||||
abc-###-xyz
|
||||
*/
|
||||
35
examples/regex_procedural.fun
Normal file
35
examples/regex_procedural.fun
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
// Procedural regex demo using built-in functions.
|
||||
// Shows internal power without the Regex class wrapper.
|
||||
|
||||
text = "abc-123-xyz"
|
||||
|
||||
// Full string match
|
||||
m1 = regex_match(text, "[a-z]+-[0-9]+-[a-z]+$")
|
||||
print(m1)
|
||||
|
||||
// First search with groups (prints a map)
|
||||
s1 = regex_search(text, "([0-9])([0-9])([0-9])")
|
||||
print(s1)
|
||||
|
||||
// Global replace: replace digits with '#'
|
||||
out = regex_replace(text, "[0-9]", "#")
|
||||
print(out)
|
||||
|
||||
/* Expected output:
|
||||
1
|
||||
{"match": 123, "start": 4, "end": 7, "groups": [1, 2, 3]}
|
||||
abc-###-xyz
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue