Permalink
Browse files

Changed regex() (Closes: #1123).

  • Loading branch information...
Angel Ortega
Angel Ortega committed Feb 3, 2011
1 parent d80efea commit 25383ea312d0ec93c1056a07c5135bdbbfbca2ce
Showing with 27 additions and 26 deletions.
  1. +1 −0 RELEASE_NOTES
  2. +1 −1 TODO
  3. +8 −8 doc/mpsl_overview.txt
  4. +5 −5 doc/mpsl_quickref.txt
  5. +4 −4 mpsl_f.c
  6. +2 −2 scripts/mkquickref.mpsl
  7. +6 −6 test.mpsl
View
@@ -23,6 +23,7 @@ MPSL Release Notes
- sscanf().
- map().
- grep().
+ - regex().
- sregex().
1.0.9
View
2 TODO
@@ -27,7 +27,6 @@ Pending Enhancements
* 1109: There must be possible to have executable components with
arguments in compact symbols (as in 'h.code(a1,a2).other').
* 1115: Add C++ style comments.
- * 1123: Invert regex() arguments.
Closed
------
@@ -202,5 +201,6 @@ Closed
* 1125: Invert map() arguments (Thu, 03 Feb 2011 10:51:53 +0100).
* 1126: Invert grep() arguments (Thu, 03 Feb 2011 12:23:20 +0100).
* 1124: Invert sregex() arguments (Thu, 03 Feb 2011 13:09:17 +0100).
+ * 1123: Invert regex() arguments (Thu, 03 Feb 2011 15:59:47 +0100).
Email bugs to angel@triptico.com
View
@@ -726,7 +726,7 @@ of its forms, it can be used to test if a string matches:
/* hours, minutes, seconds */
v = "07:55:35";
- if (regex("/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/", v))
+ if (regex(v, "/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/"))
print("The v variable contains a valid time\n");
The regular expression can contain flags; those are `i', for case
@@ -742,7 +742,7 @@ the `g' flag is used. If no match is found, NULL is returned.
v = "The value of PI is 3.1416, more or less";
- pi = regex("/[0-9]+\.[0-9]+/", v);
+ pi = regex(v, "/[0-9]+\.[0-9]+/");
If regex() is called without arguments, it returns the last match as an array
of two elements; the first contain the offset of the matched strings, and the
@@ -763,7 +763,7 @@ This can be used to extract many values from the same string:
s = "There is 10 and 35 and 42 in the number 103542!";
offset = 0;
- while (n = regex("/[0-9]+/", s, offset)) {
+ while (n = regex(s, "/[0-9]+/", offset)) {
local c;
/* print result */
@@ -779,10 +779,10 @@ This can be used to extract many values from the same string:
Though, for this particular example, the `g' flag could be more suitable:
/* return all numbers in one step */
- foreach (s, regex('/[0-9]+/g', s))
- print(s, "\n");
+ foreach (i, regex(s, '/[0-9]+/g'))
+ print(i, "\n");
-For more complex situations, the first argument of regex() can be an array
+For more complex situations, the second argument of regex() can be an array
of regular expressions (instead of a string). Those expressions will tried
one after the other on the string, each one using the offset returned by
the previous one. If all of them are matched, an array with the matched strings
@@ -794,11 +794,11 @@ is returned.
v3 = "key3=333 ";
/* array regex: a regex for each part */
- r = regex([ "/\s*/", /* possible leading blanks (0) */
+ r = regex(v1, [ "/\s*/", /* possible leading blanks (0) */
"/[a-z0-9]+/i", /* the key (1) */
"/\s*=\s*/", /* optional blanks, the = , more blanks (2) */
"/[a-z0-9]+/i" /* the value (3) */
- ], v1);
+ ]);
if (r != NULL) {
/* the key is the element #1 and the value the #3 */
View
@@ -148,10 +148,10 @@ sub func(a) { ... } of ary and returns an array
ary2 = map(ary, hash); Uses the elements of ary as
keys of hash and returns an
array with the values
-ary2 = grep(rx, ary); Matches the regex on all
+ary2 = grep(ary, rx); Matches the regex on all
elements in ary and returns
all the matching ones
-ary2 = grep(func, ary); Executes func on all elements
+ary2 = grep(ary, func); Executes func on all elements
sub func(a) { ... } of ary and returns a new array
with all elements on which
func returned true
@@ -199,15 +199,15 @@ chdir(dir); Change current directory
*Regular expression functions
-s = regex(reg, str); Matches the reg regexp into
+s = regex(str, reg); Matches the reg regexp into
str, returns the matching
substring
-s = regex(r, str, off); The same, but starting at off
+s = regex(str, r, off); The same, but starting at off
offset into str
ary = regex(); Returns the last matching
offset and size as a
two element array
-ary2 = regex(ary, str); Matches an array of regular
+ary2 = regex(str, ary); Matches an array of regular
expressions, returns as many
matches as elements in ary
res = sregex(str, reg, Matches the reg regexp into
View
@@ -749,9 +749,9 @@ static mpdm_t F_pclose(F_ARGS)
/**
* regex - Matches a regular expression.
+ * @v: the value to be matched
* @r: the regular expression
* @ra: an array of regular expressions
- * @v: the value to be matched
* @offset: offset from the start of the value
*
* Matches a regular expression against a value. Valid flags are `i',
@@ -780,9 +780,9 @@ static mpdm_t F_pclose(F_ARGS)
* is returned.
* [Regular Expressions]
*/
-/** string = regex(r, v); */
-/** string = regex(r, v, offset); */
-/** array = regex(ra, v); */
+/** string = regex(v, r); */
+/** string = regex(v, r, offset); */
+/** array = regex(v, ra); */
/** array = regex(); */
static mpdm_t F_regex(F_ARGS)
{
View
@@ -68,11 +68,11 @@ while (line = read(STDIN)) {
sregex("/\)/g", '\)');
/* leading asterisk? it's a title */
- if ((l = regex([ "/^\*/", "/.*$/" ], line)) && size(l) == 2)
+ if ((l = regex(line, [ "/^\*/", "/.*$/" ])) && size(l) == 2)
print("(", l[1], ") qr_title\n");
else
/* text-tabs-text: two columns */
- if ((l = regex([ "/^[^\t]*/", "/\t+/", "/.*$/" ], line)) && size(l) == 3)
+ if ((l = regex(line, [ "/^[^\t]*/", "/\t+/", "/.*$/" ])) && size(l) == 3)
print("(", l[0], ") (", l[2], ") qr_2cols\n");
else
/* other: two columns, with empty second one */
View
@@ -216,17 +216,17 @@ r = "|/\*[^*]*\*+([^/*][^*]*\*+)*/|";
sub substr(s, o, l) { local w=splice(s, NULL, o, l); return(w[1]); }
-v = regex(r, source);
+v = regex(source, r);
dump(v);
/* get last matching coords */
c = regex();
-v = regex(r, source, c[0] + c[1]);
+v = regex(source, r, c[0] + c[1]);
dump(v);
/* get last matching coords */
c = regex();
-v = regex(r, source, c[0] + c[1]);
+v = regex(source, r, c[0] + c[1]);
dump(v);
/* assignations as expressions */
@@ -243,7 +243,7 @@ if(f = open("config.h", "r"))
print("\nAssignations as expressions: looped regex matching\n");
c = 0;
-while(v = regex(r, source, c))
+while(v = regex(source, r, c))
{
print(v, "\n");
c = regex();
@@ -323,7 +323,7 @@ r = "/\w+/";
c = 0;
print("Words:\n");
-while(w = regex(r, v, c))
+while(w = regex(v, r, c))
{
print(w, "\n");
c = regex();
@@ -334,7 +334,7 @@ r = "/'[^']*'/";
c = 0;
print("Quotes:\n");
-while(w = regex(r, v, c))
+while(w = regex(v, r, c))
{
print(w, "\n");
c = regex();

0 comments on commit 25383ea

Please sign in to comment.