Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 47 lines (38 sloc) 1.324 kb
e63fcf2 @johnlepikhin first commit
johnlepikhin authored
1 Regexp matching on character streams. The main idea is to provide a simple regular expressions library which takes characters not from string but from function "get":
2
3 val get: unit -> char Lwt.t
4
5 Primitives supported:
6 "c" Matches character "c"
7 "[abc]" Matches any of listed characters
8 "[^abc]" Matches any of NOT listed characters
9 "[\\^\\]]" Matches "^" or "]"
10 "[^\\^\\]]" Matches NOT "^" and NOT "]"
11 "(grp)" Grouping. Matched group can be extracted from result using function "nth".
12 "." Matches any character. Be careful, Rex regexps have no looking-forward algorithms. Regexps such as ".+a" will not ever stop.
13
14 Repetition primitives:
15 "c" Matches exactly one character "c"
16 "c?" Matches zero or one "c"
17 "c*" Matches zero or more of "c"'s
18 "c+" Matches one or more of "c"'s
19 "c{2,5}" Matches 2..5 of "c"'s
20
21 Usage example:
22
23 let regexp = Rex.regexp "GET ([^ ]+) HTTP"
24
25 let get =
26 let s = "GET /some/uri HTTP/1.1\n" in
27 let pos = ref 0 in
28 fun () ->
29 let r = s.[pos] in
30 incr pos;
31 Lwt.return r
32
33 let match =
34 lwt r = Rex.apply ~get regexp in
35 let result =
36 match r with
37 | None ->
38 "no match"
39 | Some r ->
40 let uri = Rex.nth r 0 in
41 "URI=" ^ uri
42 in
43 Printf.printf "result is: %s\n" result
44
45 let _ =
46 Lwt_unix.run match
Something went wrong with that request. Please try again.