EReg object represents regular expressions. Based on PCRE (http://www.pcre.org/)
Can be created using new method:
Lib.Sys.EReg.new(r, opt)
|
Creates a new regular expression with pattern r and modifiers opt. This is equivalent to the shorthand syntax ~/r/opt If r or opt are nil, the result is unspecified.
|
r - regular expression
opt - modifiers, options
|
Created EReg object has methods:
match(s)
|
Tells if this regular expression matches string s. This method modifies the internal state. If s is nil, the result is unspecified.
|
s - text for matching
|
matched(n)
|
Returns the matched sub-group n of EReg. This method should only be called after .match or .matchSub, and then operates on the string of that operation. The index n corresponds to the n-th set of parentheses in the pattern of this EReg. If no such sub-group exists, an exception is thrown. If n equals 0, the whole matched substring is returned.
|
n - sub-group number, index
|
matchedLeft()
|
Returns the part to the left of the last matched substring. If the most recent call to .match or .matchSub did not match anything, the result is unspecified. If the global g modifier was in place for the matching, only the substring to the left of the leftmost match is returned. The result does not include the matched part.
|
|
matchedRight()
|
Returns the part to the right of the last matched substring. If the most recent call to .match or .matchSub did not match anything, the result is unspecified. If the global g modifier was in place for the matching, only the substring to the right of the leftmost match is returned. The result does not include the matched part.
|
|
matchedPos()
|
Returns the position and length of the last matched substring, within the string which was last used as argument to .match or .matchSub. If the most recent call to .match or .matchSub did not match anything, the result is unspecified. If the global g modifier was in place for the matching, the position and length of the leftmost substring is returned.
|
|
matchSub(s, pos, len)
|
Tells if this regular expression matches a substring of String s. This function expects pos and len to describe a valid substring of s, or else the result is unspecified. To get more robust behavior, .match(string.sub(s, pos, pos+len)) can be used instead. This method modifies the internal state. If s is nil, the result is unspecified.
|
s - text for matching
pos - position for substring of s
len - size for substring of s
|
split(s)
|
Splits string s at all substrings EReg matches. If a match is found at the start of s, the result contains a leading empty string "" entry. If a match is found at the end of s, the result contains a trailing empty string "" entry. If two matching substrings appear next to each other, the result contains the empty string "" between them. By default, this method splits s into two parts at the first matched substring. If the global g modifier is in place, s is split at each matched substring. If s is nil, the result is unspecified.
|
s - text for splitting
|
replace(s, by)
|
Replaces the first substring of s which EReg matches with by. If EReg does not match any substring, the result is s. By default, this method replaces only the first matched substring. If the global g modifier is in place, all matched substrings are replaced. If by contains $1 to $9, the digit corresponds to number of a matched sub-group and its value is used instead. If no such sub-group exists, the replacement is unspecified. The string $$ becomes $. If s or by are nil, the result is unspecified.
|
s - replacing text
by - matches text
|
map(s, f)
|
Calls the function f for the substring of s which EReg matches and replaces that substring with the result of f call. The f function takes EReg object as its first argument and should return a replacement string for the substring matched. If EReg does not match any substring, the result is s. By default, this method replaces only the first matched substring. If the global g modifier is in place, all matched substrings are replaced. If s or f are nil, the result is unspecified.
|
s - text for matching
f - function with string result for replacement
|
Examples:
ereg = Lib.Sys.EReg.new("('(?:\\\\.|[^\\\\\\n'])*')", "")
text = "123'test1' 456 'test2'end"
len = string.len(text)
if ereg.matchSub(text, 0, len) then
pos = ereg.matchedPos();
value = ereg.matched(0);
print(pos.pos.." "..pos.len)
print(value)
if ereg.matchSub(text, pos.pos + pos.len, len - (pos.pos + pos.len)) then
print(ereg.matched(0))
end
end
Created with the Personal Edition of HelpNDoc: Free Web Help generator