hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

README (1632B)


      1 The regex module provides an implementation of regular expressions which adheres
      2 closely to the POSIX Extended Regular Expressions (ERE) specification.
      3 
      4 See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04
      5 
      6 This module refers to a regular expression "match" as a [[result]]. The POSIX
      7 match disambiguation rules are used; the longest of the leftmost matches is
      8 returned. This implementation computes matches in linear time.
      9 
     10 Compiling an expression:
     11 
     12 	const re = regex::compile(`[Hh]a(rriet|ppy)`)!;
     13 	defer regex::finish(&re);
     14 
     15 Testing an expression against a string:
     16 
     17 	assert(regex::test(&re, "Harriet is happy"));
     18 
     19 Finding a match for an expression in a string:
     20 
     21 	const result = regex::find(&re, "Harriet is happy");
     22 	defer regex::result_free(result);
     23 	for (let i = 0z; i < len(result); i += 1) {
     24 		fmt::printf("{} ", result[i].content)!;
     25 	};
     26 	fmt::println()!;
     27 	// -> Harriet rriet
     28 
     29 Finding all matches for an expression in a string:
     30 
     31 	const results = regex::findall(&re, "Harriet is happy");
     32 	defer regex::result_freeall(results);
     33 	for (let i = 0z; i < len(results); i += 1) {
     34 		for (let j = 0z; j < len(results[i]); j += 1) {
     35 			fmt::printf("{} ", results[i][j].content)!;
     36 		};
     37 		fmt::println()!;
     38 	};
     39 	// -> Harriet rriet; happy ppy
     40 
     41 Replacing matches for an expression:
     42 
     43 	const re = regex::compile(`happy`)!;
     44 	const result = regex::replace(&re, "Harriet is happy", `cute`)!;
     45 	// -> Harriet is cute
     46 
     47 Replacing with capture group references:
     48 
     49 	const re = regex::compile(`[a-z]+-([a-z]+)-[a-z]+`)!;
     50 	const result = regex::replace(&re, "cat-dog-mouse; apple-pear-plum",
     51 		`\1`)!;
     52 	// -> dog; pear