⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trex2.jc

📁 The JILRunOnly project is a simple command-line application written in ANSI-C that is intended to de
💻 JC
字号:
/*
 *	trex2.jc
 *
 *	Test and demonstrate the Trex (tiny regular expression) native type.
 */

import stdlib;
import Trex;

/* The text for testing Search() */

const string kSearchText =
	"Trex (Tiny-REgular-EXpression) is a simple, yet powerful regular expression\n"
	"search algorithm, developed by Alberto Demichelis. The cool thing about regular\n"
	"expressions is that you can extract or convert whole text files with one\n"
	"search expression, saving you hours of painful work, if you had to do the\n"
	"same by hand!";

function string main(const array& args)
{
	stdlib::Print("* Search() test: Find all words that begin with a small 'a' or capital 'T' "
		"and return them in an array. Source text is:\n");
	stdlib::Print("\n\"" + kSearchText + "\"\n\n");

	stdlib::Print("* Creating regular expression object aWords with \"[^a-zA-Z](a|T)([a-zA-Z]+)\"\n");
	Trex aWords = /"[^a-zA-Z](a|T)([a-zA-Z]+)"/;
	if( aWords.valid )
		stdlib::Print("The regular expression is valid.\n");
	else
		stdlib::Print("There seems to be an error in the regular expression. It is invalid.\n");

	array& result = aWords.Search( kSearchText, "$1-$2" );
	stdlib::Print("* Listing resulting string array: (We insert a dash after the first character)\n");
	for( long i = 0; i < result.length; i++ )
		stdlib::Printf("result[%2d] = \"%s\"\n", {i, result[i]});
	stdlib::Print("* Done\n\n");

	stdlib::Print("* Slice() test: Slice the text into strings and put them into an array.\n");
	stdlib::Print(/"* Creating regular expression object aDelimiters with "[ ,!\t\n\-\.\(\)]+""/ "\n");
	Trex aDelimiters = /"[ ,!\t\n\-\.\(\)]+"/;
	if( aDelimiters.valid )
		stdlib::Print("The regular expression is valid.\n");
	else
		stdlib::Print("There seems to be an error in the regular expression. It is invalid.\n");

	array& slices = aDelimiters.Slice( kSearchText );
	stdlib::Print("* Listing resulting string array:\n");
	for( long j = 0; j < slices.length; j++ )
		stdlib::Printf("slices[%2d] = \"%s\"\n", {j, slices[j]});
	stdlib::Print("* Done\n\n");

	stdlib::Print("* Replace() test: Encapsulate all words in curly braces.\n");
	stdlib::Print("* Creating regular expression object allWords with \"([^a-zA-Z]?)([a-zA-Z]+)\"\n");
	Trex allWords = /"([^a-zA-Z]?)([a-zA-Z]+)"/;
	if( allWords.valid )
		stdlib::Print("The regular expression is valid.\n");
	else
		stdlib::Print("There seems to be an error in the regular expression. It is invalid.\n");
	string replaced = allWords.Replace( kSearchText, "$1{$2}" );
	stdlib::Print("* Result of Replace():\n\n");
	stdlib::Print( replaced );

	return "";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -