Capturing groups & Non-capturing groups in the regex

Hardeep Kaur
1 min readMar 8, 2023

--

Recently, I’ve been working on something which includes regex, but I’m surprised that I didn’t know about capturing & non-capturing groups after years of using regular expressions in java.

Capturing groups

Capturing groups are a way to treat multiple characters as a single unit. They are enclosed within parentheses.

For example : “(.*)(\\d+)(.*)”

Non-Capturing groups

The non-capturing group provides the same functionality of a capturing group but it does not captures the result. A non-capturing group starts with (?: and ends with ).

For example, if you need to match a URL or a phone number from a text using groups since the starting part of the desired substrings is the same you need not capture the results of certain groups in such cases you can use non-capturing groups.

To understand non-capturing groups, we can follow this example

/(Raj|Rohan|Ravi)\s(.*?)\s(Singh|Shetty)/

The regular expression above defines that I’m looking for a very particular name combination. The name should begin with Raj, Rohan, or Ravi, and end with Singh or Shetty but include a middle name between the first and last name.

Ex: Raj Preet Singh, Rohan Kumar Shetty

Conclusion ~

We can use the (?:) syntax to match non-capturing groups in your strings. Non-capturing groups are excluded from the result.

When dealing with complex regular expressions, this feature is beneficial because it simplifies the result handling! 🎉

--

--