A non-capturing group will match "x" in (?:x), however the match won't be stored, meaning you won't be able to access it using backreference, and it won't be output separately when using re.findall().
Let's see an example of the difference between capturing and non-capturing groups:
txt = "red car, blue car, yellow car, green car, red car, red bike, blue bike"
pattern1 = "(red|blue) car"
pattern2 = "(?:red|blue) car"
pattern3 = "red|blue car"
pattern4 = "((red|blue) (car|bike))"
pattern5 = "((red|blue) (?:car|bike))"
re.findall(pattern1, txt) ➞ ["red", "blue", "red"]
re.findall(pattern2, txt) ➞ ["red car", "blue car", "red car"]
re.findall(pattern3, txt) ➞ ["red", "blue car", "red", "red"]
re.findall(pattern4, txt) ➞ [("red car", "red", "car"), ("blue car", "blue", "car"), ("red car", "red", "car"), ("red bike", "red", "bike"), ("blue bike", "blue", "bike")]
re.findall(pattern5, txt) ➞ [("red car", "red"), ("blue car", "blue"), ("red car", "red"), ("red bike", "red"), ("blue bike", "blue")]
Write a regular expression to match any article + noun pair. The articles are either "a/an" or "the". Use non-capturing groups in your expression.
txt = "There is an apple and a pen on the desk"
pattern = "yourregularexpressionhere"
re.findall(pattern, txt) ➞ ["an apple", "a pen", "the desk"]
import re from the code.