1
2/** @module regexp */
3
4/**
5 * @name regexp.Replace
6 * @param {String} string
7 * @param {String} re - search for this expression
8 * @param {String} repl - replace each occurence of re with repl
9 * @returns {Dictionary} a dictionary containing split strings
10 */
11lfunction regexp.Replace(string, re, repl) {
12    return string ^ {{re,repl}};
13}
14
15/**
16 * @name regexp.Split
17 * @param {String} string
18 * @param {String} re - the regular expression
19 * @returns {Dictionary} a dictionary containing split strings
20 */
21lfunction regexp.Split(string, re) {
22    coordinates = string || re;
23    if (coordinates[0] >= 0) {
24        result = {};
25
26        current_end = 0;
27
28        for (i = 0; i < Rows(coordinates); i += 2) {
29            from = coordinates[i];
30
31            if (current_end < from) {
32                result + string[current_end][from - 1];
33            } else {
34                result + "";
35            }
36            current_end = coordinates[i + 1] + 1;
37        }
38
39        if (current_end < Abs(string)) {
40            result + string[current_end][Abs(string) - 1];
41        } else {
42            result + "";
43        }
44
45        return result;
46    }
47    return {
48        "0": string
49    };
50}
51
52/**
53 * @name regexp.Find
54 * @param {String} string
55 * @param {String} re - the regular expression
56 * @returns {String} the portion of the string that is matching
57 */
58lfunction regexp.Find(string, re) {
59    //console.log (string);
60    coordinates = string $ re;
61    if (coordinates[0] >= 0) {
62        return string[coordinates[0]][coordinates[1]];
63    }
64    return None;
65}
66
67/**
68 * @name regexp.FindSubexpressions
69 * @param {String} string
70 * @param {String} re - the regular expression
71 * @returns {AssociativeList} all matched RE subsexpressions
72 */
73lfunction regexp.FindSubexpressions(string, re) {
74    coordinates = string $ re;
75
76    if (coordinates[0] >= 0) {
77        matched = {};
78        upto = utility.Array1D (coordinates);
79        for (k = 0; k < upto; k += 2) {
80            matched + string[coordinates[k]][coordinates[k+1]];
81        }
82        return matched;
83    }
84    return None;
85}
86
87/**
88 * @name regexp.PartitionByRegularExpressions
89 * @param {Dict/Matrix} strings set of strings to partition (if Dict, will use the set of VALUES)
90 * @param {Dict/Matrix} rex - string matrix of regular expressions ((if Dict, will use the set of VALUES)
91 * @returns {Dict} "reg-exp" : "set of matched strings" (as dict) PLUS a special key ("") which did not match any regular expression
92 */
93lfunction regexp.PartitionByRegularExpressions(strings, rex) {
94    result = {};
95
96    for (_value_; in; rex) {
97        result[_value_] = {};
98    }
99
100    result[""] = {};
101    matched_regexp = None;
102
103    for (_value_; in; strings) {
104        matched_regexp = None;
105        for (re; in; rex) {
106            matched_regexp = regexp.Find (_value_, re);
107            if (matched_regexp != None) {
108                break;
109            }
110        }
111        if (None == matched_regexp) {
112            result[""] + _value_;
113        } else {
114            result[re] + _value_;
115        }
116    }
117
118
119    return result;
120}
121
122/**
123 * @name regexp.PartitionByRegularExpressionsWithSub
124 * partition the set of strings by regular expressions; if a regular expression
125 * contains subexpressions, then these will be added to the matched pattern
126 * @param {Dict/Matrix} strings set of strings to partition (if Dict, will use the set of VALUES)
127 * @param {Dict/Matrix} rex - string matrix of regular expressions (if Dict, will use the set of VALUES)
128 * @returns {Dict} "reg-exp" : "set of matched strings" (as dict) PLUS a special key ("") which did not match any regular expression
129 */
130lfunction regexp.PartitionByRegularExpressionsWithSub(strings, rex) {
131    result = {};
132    result[""] = {};
133    matched_regexp = None;
134    subs = None;
135
136    for (_value_; in; strings ) {
137        matched_regexp = utility.First (rex, "_regex_", "None!=regexp.Find ('" + _value_ +"', _regex_)");
138        if (None == matched_regexp) {
139            result[""] + _value_;
140        } else {
141            subs = Join ("|",regexp.FindSubexpressions (_value_, matched_regexp));
142            if (utility.Has (result,subs,"AssociativeList") == FALSE) {
143                result[subs] = {};
144            }
145            result[subs] + _value_;
146        }
147    }
148
149    return result;
150}
151
152
153
154