For the latest version of JMP Help, visit JMP.com/help.


Scripting Guide > Data Tables > Work with Columns > Create a New Column by Text Matching
Publication date: 07/24/2024

Create a New Column by Text Matching

The New Column by Text Matching message enables you to use regular expressions (which identify patterns in text) to match text in a column and then create a new column from the matched text. This feature is an alternative to using the Text Explorer platform to write regular expressions and then save the results to a column.

The following example shows how to select cat, cats, dog, and dogs in a column and put the values in new columns.

dt = Open( "$SAMPLE_DATA/Pet Survey.jmp" );
dt << New Column by Text Matching(
	Column( :Survey Response ),
	Set Regex(
		Custom(
			Title( "Cat" ),
			/* finds "cat", optionally followed by "s",
				with either a space or period afterward. */
			Regex( "(cat(?:s?))([\s\.])" ),
			Result( "\[\1]\" ), // results in the value matched by (cat...)
		),
	),
	Output Column Name( "Instances of Cats" )
);
dt << New Column by Text Matching(
	Column( :Survey Response ),
	Set Regex(
		Custom(
			Title( "Dog" ),
			Regex( "(dog(?:s?))([\s\.])" ),
			Result( "\[\1]\" ),
		),
	),
	Output Column Name( "Instances of Dogs" )
);

For more information about using regular expressions in JSL, see “Regular Expressions”. To learn more about regular expressions, visit Regular-Expressions.info.

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).