Use of regular expression in Apex

By | May 27, 2015

Regular expressions (or regex) are mainly used to check the Format and syntax. In simple terms, regex is a string that is used to compare another string by using a specific syntax. Similar to many other languages, Apex also provides regex capabilities.

Read More: Real-time, bi-directional, multi-company – Sage ERP Integration with Salesforce

Regex functionality in Apex can be achieved in the same manner as in Java i.e. with the help of Pattern and Matcher classes. Apex supports the standard syntax for regular expression used in java. A pattern is compiled form of a regular expression which can be used by the matcher to verify the format of the string.

Below is the sample example to validate the ZIP code for US format –


In this example, we have created a method to validate the ZIP code and this can be reused multiple times.

But if one wants to use the regex only once, the same can be achieved in a single sentence as shown below –


About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data MigrationIntegrated App developmentCustom App development and Technical Support to business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.

For more information on our Salesforce products and services, contact us at We will be glad to assist you.

Related Posts