Applications Services Blog
Get the latest thought leadership and information about the role of Applications Services in an increasingly interconnected world at the HP Blog Hub.

Extracting data with regular expressions in Microsoft Dynamics CRM 2011 processes

In last week’s blog post, I showed how to use regular expressions in Microsoft Dynamics CRM 2011 processes for data validation. Today I'll show you how you can use a similar approach to parse text and extract matching strings using regular expressions in a Dynamics CRM 2011 process.


If you're unfamiliar with regular expressions, I recommend you take a look at my earlier post for an idea of how they work. You may also want to take a look at this MSDN article about regular expressions work in .Net.


In my validation example, I used a custom workflow activity to do the following:

  1. Accept a string to validate and a regular expression match pattern via input arguments.
  2. Evaluate the regular expression.
  3. Return the result of the match via an output argument.

For extracting matching data, we will need a custom workflow activity that does this instead:

  1. Accept the following input arguments:
    • A string to parse for matches
    • A regular expression match pattern to evaluate
    • A parameter that specifies whether to return the first match, last match or all matches. (The .Net regular expression engine creates an array of matched strings, so we have some flexibility in what we can return.)
    • A string separator to use in a concatenated string if all matches are returned
  2. Evaluate the regular expression.
  3. Return the resulting match(es) via an output argument.

The code

First we set up our input and output arguments like so:

/// <summary>
/// This is the string that will be parsed
/// </summary>
[Input("String to parse")]
public InArgument<String> StringToParse { get; set; }

/// <summary>
/// This is the regular expression used for extraction - see C# regex quick reference here -
/// </summary>
[Input("Match pattern")]
public InArgument<String> MatchPattern { get; set; }

/// <summary>
/// This specifies whether to return the first match, the last match or all matches (in a concatenated string). Acceptable values are First|Last|All.
/// </summary>
[Input("Return type")]
public InArgument<String> ReturnType { get; set; }

/// <summary>
/// If "All" is specified as extraction type, this used as a string separator in the concatenated extract string
/// </summary>
[Input("String separator")]
public InArgument<String> StringSeparator { get; set; }

/// <summary>
/// This returns the matching string if one is found or an empty string if a match is not found
/// </summary>
[Output("Extracted string")]
public OutArgument<string> ExtractedString { get; set; }

Next we create an Enum type to represent the extraction type (first|last|all).

enum ExtractionType

Then we get all of our inputs ready to use:

//string to parse
string parseString = StringToParse.Get(executionContext);

//pattern to match
string matchPattern = MatchPattern.Get(executionContext);

//type of match to be returned - first match, last match or all matches
ExtractionType extractType;
switch (ReturnType.Get(executionContext).ToUpperInvariant())
	case "FIRST":
		extractType = ExtractionType.First;
	case "LAST":
		extractType = ExtractionType.Last;
	case "ALL":
		extractType = ExtractionType.All;
		//default will return first match only
		extractType = ExtractionType.First;

//separator to be used for an "all" match
string stringSeparator = StringSeparator.Get(executionContext);

Finally we use this method to handle the actual matching and string extraction:

/// <summary>
/// method to evaluate a regular expression and return the first match, last match or all matches in a concatenated string
/// </summary>
/// <param name="parseString">string to parse</param>
/// <param name="matchPattern">regular expression to evaluate</param>
/// <param name="extractType">match(es) to return - first|last|all</param>
/// <param name="separator">string to use as a separator for an "all" match return type</param>
/// <returns></returns>
private string ExtractMatchingString(string parseString, string matchPattern, ExtractionType extractType, string separator)
	//set the default output to the empty string. if we match something, we'll change it.
	string output = string.Empty;

	//do the regex match
	MatchCollection matches = Regex.Matches(parseString, matchPattern);
	if (matches.Count > 0)
		//which match(es) should we return?
		switch (extractType)
			case ExtractionType.First:
				output = matches[0].Value;
			case ExtractionType.Last:
				output = matches[matches.Count - 1].Value;
			case ExtractionType.All:
				StringBuilder matchingSb = new StringBuilder();
				for (int i = 0; i < matches.Count; i++)
					if (i != matches.Count - 1)
				output = matchingSb.ToString();
	return output;

Here's how we call it and return the result to the calling process:

//evaluate the regex and return the match(es)
	string extractedString = ExtractMatchingString(parseString, matchPattern, extractType, stringSeparator);
	ExtractedString.Set(executionContext, extractedString);
catch (Exception e)
	tracingService.Trace("Exception: {0}", e.ToString());

A full class file is attached at the bottom of this post (


Seeing it in action

I set up a simple dialog that takes a user input and then parses it for U.S. phone numbers to display to the user. The dialog calls the custom workflow activity three times to get the first match, last match and all matches. Here's how I configured the inputs for the "all" scenario:




When I run the dialog, here's the first page where the input is entered:




And here's what the dialog displays to the user after the matching is complete. You'll see each type of match string is displayed on a separate line:




Could you see your organization using this to improve its Dynamics CRM business processes? What sorts of efficiencies do you think regular expression string extraction could help you achieve? Let us know in the comments!




Leave a Comment

We encourage you to share your comments on this post. Comments are moderated and will be reviewed
and posted as promptly as possible during regular business hours

To ensure your comment is published, be sure to follow the community guidelines.

Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
Type the characters you see in the picture above.Type the words you hear.
About the Author
Microsoft Dynamics CRM solution architect, C# developer, MBA, husband, father, Auburn man. Follow me on Twitter @lucas_is.
About the Author(s)
  • Business Development leader for HP’s Testing Practice in Americas and TaaS Global Capability Leader
  • Anthony works in the ES Apps Enterprise APJ . He is a self-motivated and well-disposed individual. He is a certified SAP FICO consultant. He has over twenty years industry experience in business transformation and Information Technology consulting (SAP). With excellent, interpersonal and communication skills, Anthony is able to influence and motivate at all levels. An empathic team player, He derives fulfilment in contributing towards others' development. Anthony possesses good analytical and problem-solving skills. He is a quick thinker, remains calm under pressure, handles multiple tasks, and accomplishes objectives with excellent time management. He is flexible and accustomed to meeting high standards and deadlines. Anthony possesses practical knowledge of business processes and concepts and has a keen eye for detail. He is aware of the importance of an effective system and quality management.
  • Bryan Coapstick is the Director of Mobile Innovation at HP. In this role, he is responsible for driving strategic mobility initiatives and key industry partnerships to enable clients the ability to leverage the mobile channel to effectively reach their customers. Bryan’s expertise lies with several Fortune 200 companies in both public and private sectors that focus on business technology and emerging strategies. Bryan demonstrates innovative leadership though public speaking, panel discussions and blogging on the various ways mobility is changing our daily lives.
  • Benjamin Romberg has over twenty five years experience in the IT industry, the last twelve of which have been as a testing professional. He has experience as a test analyst, performance tester, automation specialist, test lead, test manager, test consultant and test executive. He has worked on all types of testing engagements ranging from small web based applications through to core system upgrades. Benjamin holds a Bachelor’s Degree in Genetics, a Post Graduate Degree in Software Engineering and an Executive Masters in Business Administration. Benjamin now leads HP’s Testing and Quality Assurance Practice in Asia Pacific and Japan.
  • Over the past 5 years, I have participated in the different stages of delivering Microsoft Enterprise Applications to customers as large as 40.000 concurrent users across Europe with a focus on the public and call center sector. During this period, I worked in numerous technical (lead) and business consultant roles. Using this experience, I am currently a certified Microsoft Dynamics CRM 2011 expert which specializes in guiding enterprise customers and implementation teams through the process of implementing Microsoft Dynamics CRM 2011.
  • Craig has been with HP for 10 years working on the solutioning side as a Solution Architect. Primarily works with Microsoft technologies and has for the better part of 16 years. This past year Craig has worked mostly with Windows AZURE and the mobility side of Windows Phone 7/8 and Android.
  • Master Business Consultant and Distinguished SE with 35+ years of IT experience, specializing in SOA and Integration.
  • Daniel Amor (EMEA, AMOD, Cloud, Portals, Web Apps and SOA domain expert): Daniel has designed, won, led and delivered large-scale, complex applications-related projects by acting as a trusted advisor to clients. Daniel conducts regular training and client-facing sessions, and he has authored six books and numerous articles in European and US-based magazines and newspapers on IT change. He is a regular lecturer and he speaks on Portals, E-Business and Cloud.
  • Global HP Microsoft Business Applications Practice. Microsoft Dynamics CRM subject matter expert. Based in Manchester, United Kingdom. @DarrenCRM on Twitter
  • Solutions Architect working at HP since 2002 with focus on Applications Modernization, located at Rio de Janeiro, Brazil
  • Managing Consulting specialising in Business Transformation and Value Management for Defence, Healthcare, Public Sector, Criminal Justice and Telecommunications.
  • Guido Arndt is since May 1997 business consultant and project manager @ Hewlett Packard Enterprise Services. Main focus is strategic and organizational alignment of logistics, sales and production processes in the automotive and manufacturing industry as well as business performance projects in finance and telecommunication industry. Guido has experience as change agent and transformation manager in people-driven merger & aquisition projects focusing government, consulting firms, manufacturing plants and consumer brands.
  • Horia Slușanschi serves as an Agile Coach leading the HP Agile Mentoring Office and the HP Software Engineering Profession. He is passionate about helping teams and leaders to find joy in work in the pursuit of customer delight.
  • CISSP, Open CA Master Architect Technical Consultant with 28+ years of IT experience specializing in Application Security, Solution Architecture, Portals, Social Business, and Integration
  • Jim has been with HP for 33 years and currently leads a global Strategic Capabilities Management team, with a specific focus on Business Analysis and Configuration Management. Jim also manages a team within the US State, Local, and Education division of HP. He was a member of the IIBA committee that created a Business Analyst Competency Model and he participated in the development of IEEE standards. Jim graduated from Harvard University, the University of British Columbia, and Ottawa Theological Hall. He currently lives in Toronto, Canada.
  • SAP Technologist from APPS GD SAP Practice - Manila covering SAP NetWeaver EP, SAP Mobility and general SAP solutions.
  • WW product marketing manager for HP Applications Modernization and Transformation Consulting Services. 35+ years of IT technology and services experience.
  • Laurence has over 35 years experience in the IT industry and has spent 27 years delivering knowledge management (and before that became fashionable, Artificial Intelligence and knowledge-based systems) transformation projects to clients in Government, Industry, Manufacturing and Telecomms industries. He has developed and implemented knowledge management (KM) strategies for HP's clients and for business areas in HP (for BPO and for our Consulting business) and believes that KM strategy is essential for an effective KM practice.
  • . . Service Systems . .
  • Lori Lewis leads Global Digital and Integrated Marketing for the Applications and Business Services division in HP’s Enterprise Services. The organization is focused on providing clients with enterprise applications services and solutions to solve some of their most pressing business problems. For over 2 decades, she has championed innovative communication approaches to drive and grow market share and build motivated, empowered teams. She is a compelling, passionate ambassador of the art and science of marketing and the amazing results it can achieve when done with integrity and passion. Ms. Lewis’ credentials, track record, and enthusiasm are essential ingredients her team relies on as they work together to establish HP as the undisputed leader in information management and analytics services.
  • Microsoft Dynamics CRM solution architect, C# developer, MBA, husband, father, Auburn man. Follow me on Twitter @lucas_is.
  • CRM enthusiast, avid techie
  • More than 10 years of experience in developing effective and innovative marketing and communication strategies. Recently I have studied the effects of social media and disruptive technology on business, marketing, and culture. Follow me on Twitter: @micaelaraimondi
  • Agile Champion and coach. Passion in software development using lean and agile techniques. Software is a people business!!
  • Have 11 years of experience in SAP Consulting, Solutioning and Account management.
  • I have over 20 years of I.T. experience predominantely in applications development. In addition to my I.T. experience I spent 7 years as the Director of Store Services at Canada's third largest grocery retailer.
  • Shafraz Nasser is the Lead Microsoft CRM Architect with HP Enterprise Services in the South Pacific Microsoft Dynamics Practice. His focus is on selling, architecting and overseeing delivery of Microsoft Dynamics CRM based implementations within New Zealand.
  • Shefali Dua is leading Worldwide BPaaS Strategy & Solution Innovation team at HP Enterprise Services as the Solution Innovation Manager. Her focus is currently assisting in planning and delivery of industry solutions for verticals like social media, big data, mobility and security on the HP Enterprise Cloud offerings for CRM Technologies.
  • Steve Petruno is a Product Manager for HP’s Global Oracle Services with over 24 years experience in the Information Technology industry, project Management, Human Resources, Payroll and Benefits, ERP implementations, upgrades, acquisitions and divestitures.
  • I have worked for HP as a business consultant for seven years, providing process, strategy and business transformation advice in the Financial Services and UK Government sectors. As part of this I have used Lean and Continuous Improvement techniques to assess and improve business performance, ensure regulatory compliance and improve environmental sustainability.
  • Tom is the Global Product Marketing lead for HP's Cloud Applications Services. Tom has over 20 years of experience in information technology and a proven track record for implementing innovative client solutions in several industries including government, healthcare, and telecommunications. He thrives on developing services and solutions which result in business process improvement through greater systems efficiency.
  • Vijay Seetharaman is an Enterprise Architect within HP Enterprise Services - Industry Transformation Consulting practice, focusing on Cloud and Mobility

Follow Us