What can we help you with?

Understanding Regular Expression (Regex) pattern matching in ELEX Bulk Edit Products, Prices & Attributes for WooCommerce plugin

If you are just started out in programming or a non-technical wizard, regular expression (also known as regex) pattern matching might be a new concept to you. However, once you get hold of it, you probably will find it easier to work with.

In this article, you will get a highlight of what is regex pattern matching, its syntax and how it is used to filter & bulk edit products in our ELEX Bulk Edit Products, Prices & Attributes for WooCommerce plugin.

What is Regex pattern matching?

Regular expressions are formatted text strings that are used for text processing. They are basically developed to extract part of text/word/numbers and use it for computation. This cuts down a large code by calling a single function for text processing operations.

The regex can be used for pattern matching, replacing a search pattern with a text, and split arrays in numerous ways to get started. In a broader perspective, it is used to verify and validate texts, name, email addresses, phone number, and so on. It can also be used for translating text strings and web scraping (or web data extraction).

Regex Patterns

Regex makes use of metacharacters for pattern matching. These characters help you achieve different types of results.

Quantifiers

Following are basic quantifiers used for regex, with their usage, explanation, and sample resulting patterns.

Metacharacter(s) Usage Explanation Resulting search patterns
^ ^Wor Matches every string that begins with ‘Wor’ WordPress, Workstation, etc.
$ end$ Matches every string that ends with ‘end’ Append, Prepend, Suspend, etc.
* Wo* Matches terms that have zero or more strings having ‘Wo’ in them. WordPress, WooCommerce, Workstation, Web, Windows, etc.
/+ Com+ Matches strings that has ‘Co’ followed by one or more ‘m’. Commerce, Command, Compass, etc.
? Com? Matches strings that have ‘Co’ followed by zero or one ‘m’. Commerce, Command, Connect, Code, etc.
\ \Word Quotes the following metacharacter and matches the strings that have ‘\Word’ \Word, \WordPress


Brackets

When the above quantifiers are used with text strings inside square brackets, they are known as Character Class. Different types of results can be obtained using the brackets.

Brackets Usage Explanation Resulting search patterns
[…]
[word] Matches any text pattern that has ‘w’, ‘o’, ‘r’, ‘d’ in them. Windows, Fedora, Word, etc.
[^word] Matches any text pattern that does not have ‘w’,’o’,’r’, or, ‘d’ Any alphanumeric text string including space and numbers.
[a-z] Matches any string from lowercase ‘a’ to lowercase ‘z’. WordPress, wordpress, etc.
[A-Z] Matches any string from uppercase ‘A’ to ‘Z’. WordPress, WORDPRESS, etc.
[a-zA-Z] Matches any string that has both lowercase and uppercase alphabets. WordPress, wordpress, WORDPRESS, etc.
[^a-zA-Z] Matches strings that do not have both lowercase and uppercase alphabets. Returns text strings with special characters and empty spaces.
[0-9] Matches single digit number from 0 to 9 12345, Word111, etc.
Woo [Shirt|Hoodie] Matches strings with ‘Woo Shirt’ and ‘Woo Hoodie’ in them. Woo Shirt, Woo Hoodie, Woo shirt black, etc.
{…}
Pres{2} Matches text strings with exactly two instances of ‘s’. WordPress, Pressure, etc.
Pres{3, } Matches text strings with three or more instances of ‘s’. Returns text strings with three or more instances of the letter ‘s’ in them.
Pres{3, 5} Matches text strings with three to five instances of ‘s’. Returns text strings with three to five instances of the letter ‘s’ in them.
(…)
W(oo)* Matches strings having zero or more instances of ‘oo’. WordPress, WooCommerce, Website, etc.
W(o|e) Matches strings that have either ‘Wo’ or ‘We’. WordPress, Website, North West, Fire Works, etc.
(win|won) Matches all the strings that have ‘win’ or ‘won’. winner, windows, wonderful, slowing, etc.
([a-z]|[0-9]) Matches all strings that have lowercase alphabets and/or numbers. website, web123, 12345, etc.
([A-Z]{4}|[0-9]{3} Matches all string with at least four alphabets and/or three numbers. WORDPRESS, 12345, WORD1, TEXT123, etc.

 

Predefined Character Class

The alphabets, numbers, and whitespaces are used very frequently. Hence, there are shortcuts to use them instead of using the whole search pattern.

Class Usage Explanation Resulting search patterns
.(dot)
Wo. Matches any single character except the newline character. WordPress, WooCommerce, Workstation, etc.
w.r Matches strings with ‘w’, any single character, and ‘r’. software, word, etc.
\d Similar to [0-9] Matches all digit characters. 123, Word111, etc.
\D Similar to [^0-9] Matches all non-digit characters. Word, word123, etc, including special characters and white space.
\w Similar to [a-zA-Z_0-9] Matches lowercase letters, uppercase letters, numbers, and underscore. WORD, 123, word123, word_123, etc.
\W Similar to [^a-zA-Z_0-9] Matches only special characters and whitespaces Returns strings with only special characters and whitespaces.
\s Similar to [ \t\n\r] Matches only space, tab, newline or carriage return character. Returns strings with space, tab, newline or carriage return characters.
\S Similar to [^ \t\n\r] Matches non-whitespace characters.
Returns strings with non-whitespace characters. That is alphabets, numbers, and special characters.

 

Tip: If you want to test regex patterns, try out yourself by just enter your patterns and test string in the Online regex tester.

Regex pattern matching in ELEX Bulk Edit Products, Prices & Attributes for WooCommerce plugin

The regular expression is used in two settings of Bulk Edit Products, Prices & Attributes for WooCommerce plugin:

  1. For filtering products based on Product Title
  2. For bulk editing/updating Product Title and Product SKU.

Let us now understand regex usage by taking examples for each of the above cases.

1. Regex pattern matching for filtering products based on Product Title

My WooCommerce demo store has a lot of products titled Woo Hoodie, Woo Mug, Woo T-Shirt, Woo Single, Woo Album, Woo Logo, Ship Your Idea, Woo Ninja, Ninja Silhouette, etc as shown in the screenshot.

WooCommerce Bulk Edit Products using Regex | Available products in Demo store
Available products in Demo store

Example 1: For demonstration purpose, let us filter all the “Ship Your Idea” products. To do this, go to plugin settings, and just enter the pattern ^Ship, as shown in the screenshot below.

WooCommerce Bulk Edit Products using Regex | Filter products started with 'Ship' using Regex pattern
Filter products started with ‘Ship’ using Regex pattern

The plugin will process the filter and preview the products as shown in the below screenshot.

WooCommerce Bulk Edit Products using Regex | Ship Your Idea products filtered using Regex pattern
Ship Your Idea products filtered using Regex pattern

As you can notice, all the products that start with the term “Ship” are filtered for bulk editing.

Example 2: Let us filter all “Woo Single” and “Woo Album” products. For this, enter the pattern Woo (Single|Album) in the given Regex pattern matching text field.

WooCommerce Bulk Edit Products using Regex | Filter Woo Single and Woo album products using Regex pattern
Filter Woo Single and Woo album products using Regex pattern

Once you click Preview Filtered Products, the filtered products are as shown in the screenshot below.

WooCommerce Bulk Edit Products using Regex | Woo Single and Woo Album products filtered using Regex pattern
Woo Single and Woo Album products filtered using Regex pattern

You can notice the title of filtered products is either Woo Single or Woo Album.

Next, you can proceed with the bulk editing by clicking Continue.

2. Regex pattern matching for bulk editing/updating Product Title and Product SKU.

Once you are in the editing stage, there are a lot of settings you can make use of to bulk edit products like Product title, Product SKU, Product Visibility, Shipping Class, Sale Price, Regular Price, Stocks options, product weight, product dimensions, and product attributes.

The Regex pattern matching is available for only two settings: Product Title and Product SKU.

Let us see how we can bulk edit product title and product SKU with Regex pattern matching.

Example 1: Let us change the products with the title “Woo Album” to “Woo Music”.

The following screenshot shows the filtered products, Woo Album.

WooCommerce Bulk Edit Products using Regex | Woo Album filtered for a Bulk update
Woo Album filtered for a Bulk update

Once you click Continue and proceed with the editing, you need to select RegEx Match option in the Product Title setting. Enter pattern as Album and replacement as Music, as shown in the below screenshot.

WooCommerce Bulk Edit Products using Regex | Replacing Album with Music using Regex
Replacing the term ‘Album’ with ‘Music’ using Regex

Next, when we click Update Products and proceed with the update, the filtered products are updated with changes and are as shown in the screenshot below.

WooCommerce Bulk Edit Products using Regex | Album replaced with Music using Regex
The term ‘Album’ is replaced with ‘Music’ using Regex

Example 2: Applying product SKU in bulk for a set of products.

In the following screenshot, you can notice the product SKU for Posters are of ‘P-Demo-XX’ series and ‘P-Sample-XX’ series.

WooCommerce Bulk Edit Products using Regex | Product SKUs to be replaced using Regex
Product SKUs to be replaced using Regex

Let us replace the product SKU of all the Posters with simply ‘Poster-XX’ instead of P-Demo-XX and P-Sample-XX, with our ELEX Bulk Edit Products, Prices & Attributes for WooCommerce plugin.

To do this, go to plugin settings, and select ‘Regex Replace’ option in the SKU setting. Next, enter the pattern P-(Demo|Sample) in the Pattern text box and enter replacement as Poster, as shown in the below screenshot.

WooCommerce Bulk Edit Products using Regex | Replacing Product SKU using Regex
Replacing Product SKU using Regex

Once the update is successful, the updated products will be as shown in the screenshot below.

WooCommerce Bulk Edit Products using Regex | Product SKU replaced using Regex pattern
Product SKU replaced using Regex pattern

Hence, in this way, you can make use of Regular Expressions (Regex) pattern matching to filter products and bulk edit Product title and Product SKU using ELEX Bulk Edit Products, Prices & Attribute for WooCommerce.


To explore more details about the plugin, go check out ELEX Bulk Edit Products, Prices & Attributes for WooCommerce.

or read the product documentation to know every feature of the plugin, in detail.

You can also check out other WooCommerce and WordPress plugins in ELEX.

Previous How to Bulk Edit Products based on WooCommerce User Roles?
You must be logged in to post a comment.