Click or drag to resize

Regular Expressions

Regular expressions provide a powerful way to describe and manipulate text through pattern matching. You can use regular expressions to parse large blocks of text and find specific character patterns very quickly, to make sure that input text matches the specified format, to perform operations on substrings, etc.

gradient

 

A regular expression comprises of two types of characters:

 

Classes associated with regular expressions are contained in the Framework Class Library (FCL) namespace System.Text.RegularExpressions. The Regex class is the main class which provides methods and properties for working with regular expressions. It provides several static methods that you can call without instantiating a Regex object, however if you plan to use the same regular expression repeatedly, it's a good idea to construct a Regex object

Some of the most often used members of the Regex class are listed in the table below:

 

Method or property

Explanation

IsMatch

Indicates if the regular expression has found a match in the input string

Match

Searches an input string and returns a match for a regular expression

Matches

Searches an input string and returns all successful matches for a regular expression

Replace

Replaces all occurrences of a pattern with a replacement string

Split

Splits an input string into an array of substrings based on a regular expression

 

Note that the result of applying a regular expression to a string is either a substring or a new string which is a modification of the original string, or part of it). Since string objects in SIMPL#/C# are immutable they cannot be changed by the regular expression.

 

 

Example:

SIMPL#

C#
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using Crestron.SimplSharp;
	using System.Text.RegularExpressions;

	namespace SIMPLSDateTime
	{
		public class UStoEUDate
		{
			/// <summary>
			/// USDate is a date in format MM/DD/YY.
			/// </summary>
			public string USDate;

			/// <summary>
			/// Takes the value of USDate, and turns it into European format DD/MM/YYYY assuming YYYY>=2000
			/// </summary>
			/// <returns></returns>
			public string ConvertDate()
			{
				try
				{
					// Use the Regex.Replace function for regular expression operations
					// when you don't need to create an explicit regular expression object.
					// Note that the replacement expression ${day} inserts the substring capturedby the group (?<day>),
					// ${month} inserts the substring captured by the group (?<month>), and
					// ${year} inserts the substring captured by the group (?<year>).
					return Regex.Replace(USDate,
						  "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2})\\b",
						  "${day}/${month}/20${year}", RegexOptions.None);
				}
				catch
				{
					return USDate;
				}
			}
		}
	}

 

SIMPL+

C++
	#DEFAULT_VOLATILE
	#ENABLE_STACK_CHECKING
	#ENABLE_TRACE
	#ENCODING_ASCII

	#USER_SIMPLSHARP_LIBRARY "SIMPLSDateTime"

	DIGITAL_INPUT Trigger;

	STRING_INPUT Input$[255];

	STRING_OUTPUT Output$;

	UStoEUDate myDate;

	PUSH Trigger
	{
		Output$= myDate.ConvertDate();
	}

	CHANGE Input$
	{
		myDate.USDate=Input$;
	}

	Function Main()
	{
		// TODO:  Addcode here
		// Initialize declared globaland local variables/arrays as needed.
		// WaitForInitializationComplete();
		// If you are reading any Inputor Output variables, uncomment
		// the WaitForInitializationComplete statement above and read
		// them afterwards. Input/Output variables will not have
		// their correctvalues set until after the logic processor
		// runs and propagatesthe values to them.
	}

 

footer
Crestron Blue 400 x 100

Copyright (C) 2013 to the present, Crestron Electronics, Inc. All rights reserved. No part of this work may be reproduced in any form, machine or natural, without the express written consent of Crestron Electronics.