Русский Regex Split
tests laboratory

This method will help avoid problems connected with ^text.split[] and solve the problem of processing strings of complex structure. It is like the conventional split in that it accepts the same parameters and it returns the same object of class table which contains:

Parameter regex is a regular expression or text to define the delimiter. Parameter delimiter is optional and can acquire the following values:

The method:

########################################
# Splits the string using regular expressions
#
# l - split from left to right (by default);
# r - split from right to left;
# h - form a nameless table where the parts of the source string are aligned horizontally;
# v - form a table with column piece where the parts of the source string are aligned vertically (by default).
#####
@rsplit[text;regex;delimiter][table_split]
^if(def $text && def $regex){
	$table_split[^table::create{piece}]
	$result[^text.match[(.*?)(?:$regex)][g]{^if(def $match.1){^table_split.append{$match.1}}}]
	^if(def $result){^table_split.append{$result}}
	^if(!def $delimiter){$delimiter[lv]}
	^switch[^delimiter.lower[]]{
		^case[r;rv;vr]{$result[^table::create[$table_split;$.reverse(1)]]}
		^case[rh;hr]{$result[^table::create[$table_split;$.reverse(1)]]$result[^result.flip[]]}
		^case[h;lh;hl]{$result[^table_split.flip[]]}
		^case[DEFAULT]{$result[$table_split]}
	}
}{
	^throw[parser.runtime;rsplit;parameters ^$text and ^$regex must be defined]
}
### End @rsplit[]

Examples:

$text[12:01. 14:05, 18:00,21:20 Sports news]
Source string: $text
$parts[^rsplit[$text;[,\.]\s?]] Result: ^parts.menu{$parts.piece}[ ]
Source string: 12:01. 14:05, 18:00,21:20 Sports news
Result: 12:01 14:05 18:00 21:20 Sports news

$text[2004-12-22 13:30]
Source string: $text
$parts[^rsplit[$text;[^^0-9];r]] Result: ^parts.menu{$parts.piece}[ ]
Source string: 2004-12-22 13:30
Result: 30 13 22 12 2004

$text[2004/12//22]
Source string: $text
$parts[^rsplit[$text;/{1,2};rh]] Result: ${parts.0}.${parts.1}.$parts.2
Source string: 2004/12//22
Result: 22.12.2004
Powered by Parser 3 Eugene Spearance