# ~~~
redef fun to_s do return string.substring(from,length)
+ # The content of `string` before the match
+ #
+ # ~~~
+ # var m = "hello world".search("lo")
+ # assert m.text_before == "hel"
+ # ~~~
+ fun text_before: String do return string.substring(0, from)
+
+ # The content of `string` after the match
+ #
+ # ~~~
+ # var m = "hello world".search("lo")
+ # assert m.text_after == " world"
+ # ~~~
+ fun text_after: String do return string.substring_from(after)
+
init
do
assert positive_length: length >= 0
return null
end
+ # Extract a given prefix, if any.
+ #
+ # ~~~
+ # var p = "hello world".prefix("hello")
+ # assert p != null
+ # assert p.text_after == " world"
+ # ~~~
+ fun prefix(t: Text): nullable Match do
+ var len = t.length
+ if substring(0, len) == t then
+ return new Match(self.to_s, 0, len)
+ end
+ return null
+ end
+
+ # Extract a given suffix, if any.
+ #
+ # ~~~
+ # var p = "hello world".suffix("world")
+ # assert p != null
+ # assert p.text_before == "hello "
+ # ~~~
+ fun suffix(t: Text): nullable Match do
+ var len = t.length
+ var from = length - len
+ if substring(from, len) == t then
+ return new Match(self.to_s, from, len)
+ end
+ return null
+ end
+
# Search all occurrences of `pattern` into self.
#
# var a = new Array[Int]