Avoid unnecessary capturing groups to reduce memory consumption

We come across so many examples of regular expressions on the internet that promote unnecessary capturing groups. If we are not extracting any substring or not using a group in backreferences, then it is better to avoid capturing groups by using one or more of the following ways:

  1. We can use character classes in certain cases. Consider the following capturing group:
      (a|e|i|o|u) 

So, instead of using the preceding regex, we can use the following:

      [aeiou]
  1. We can use a non-capturing group by placing a ?: at the start of the group. Consider the following regex:
      (red|blue|white) 

Instead of the previous regex, we can use the following:

      (?:red|blue|white) 
  1. To write a regex to match an integer or decimal number there is no need to use the following regex:
      ^(d*)(.?)(d+)$ 

We can just rewrite it by removing unnecessary groups, as follows:

      ^d*.?d+$ 
  1. Sometimes, a regex may contain multiple problems, such as the ones we discussed in the previous subsection:
      ^https?://(www.)?example.com$ 

Not only does this regex use excessive escaping but there is also an unnecessary capturing group in this regex. Hence, by applying these fixes, the preceding regex can be better written as follows:

      ^https?://(?:www.)?example.com$  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.233