Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
class Regex is for Regular expression.
Supported all features of Oniguruma. See below for details.
You can use a regular expression as literal with surrounded by /.
Between / and /, you do not have to escape characters like \n.
/ab+[\t\n]/
The above example is the same as below.
new Regex("ab+[\\t\\n]"); // same as /ab+[\t\n]/
new Regex(%|ab+[\t\n]|); // same as /ab+[\t\n]/
If you want to change the character of / for a literal, any character used with the prefix of %m is available.
In that case, you do not have to escape a character except the character used with the prefix of %m.
If you use a bracket character, you can use a close character corresponding to the open character which you used.
%m<ab+[\t\n]> // same as /ab+[\t\n]/
%m(ab+[\t\n]) // same as /ab+[\t\n]/
=~ Operator and =~ OperatorThese operators are described at the document below. Please see the document below for details.
StringA regular expression object can be directly used for the following methods of a string.
replace … Use as specifying a condition string.var s = "xabbbbbcabbc".replace(/ab+/, ",");
System.println(s);
Here is the result.
x,c,c
split … Use as a separator condition.var s = "xabbbbbcabbc".split(/ab+/);
s.each(&(e) => System.println(e));
Here is the result.
x
c
c
There is a note when it is used in a condition expression of loop like while.
For example, you can loop until not matched with the string of str.
But if the loop has ended by such as break, there is a situation that the regular expression object has not correctly been reset in the start of the next loop.
while (group = (str =~ /ab+/)) {
/* block */
}
A regular expression object will be reset with the following 2 situations.
str has been changed.This is currently a restriction.
=~ Operatorvar a = "111/aaa/bbb/ccc/ddd";
while (group = (a =~ /\w+\//)) {
for (var i = 0, len = group.length(); i < len; ++i) {
System.println("found[%2d,%2d) = %s"
% group[i].begin
% group[i].end
% group[i].string);
}
}
found[ 0, 4) = 111/
found[ 4, 8) = aaa/
found[ 8,12) = bbb/
found[12,16) = ccc/
=! Operatorvar s = "XYZXYZ";
if (s !~ /ABC/) {
System.println("ABC is not included.");
}
ABC is not included.
String#replace()var s = "xabbbbbcabbc".replace(/ab+/, ",");
System.println(s);
x,c,c
String#split()var s = "xabbbbbcabbc".split(/ab+/);
s.each(&(e) => System.println(e));
x
c
c
%m Prefix (1)var a = "111/aaa/bbb/ccc/ddd";
while (group = (a =~ %m(\w+/))) {
for (var i = 0, len = group.length(); i < len; ++i) {
System.println("found[%2d,%2d) = %s"
% group[i].begin
% group[i].end
% group[i].string);
}
}
found[ 0, 4) = 111/
found[ 4, 8) = aaa/
found[ 8,12) = bbb/
found[12,16) = ccc/
%m Prefix (2)var a = "111/aaa/bbb/ccc/ddd";
while (group = (a =~ %m<\w+/>)) {
for (var i = 0, len = group.length(); i < len; ++i) {
System.println("found[%2d,%2d) = %s"
% group[i].begin
% group[i].end
% group[i].string);
}
}
found[ 0, 4) = 111/
found[ 4, 8) = aaa/
found[ 8,12) = bbb/
found[12,16) = ccc/
%m Prefix (3)var a = "111/aaa/bbb/ccc/ddd";
while (group = (a =~ %m1\w+/1)) {
for (var i = 0, len = group.length(); i < len; ++i) {
System.println("found[%2d,%2d) = %s"
% group[i].begin
% group[i].end
% group[i].string);
}
}
found[ 0, 4) = 111/
found[ 4, 8) = aaa/
found[ 8,12) = bbb/
found[12,16) = ccc/