A log analysis script needs to extract all IP addresses from a string. The IPs are in dotted-decimal format. Which regex pattern will correctly extract them?
Trap 1: r'[0-9]+\
This pattern embeds a literal space after the first escaped dot (`\. `), so it expects input like `192. 168.1.1` rather than the standard `192.168.1.1` found in server logs. Even if the space were removed, using `+` instead of `{1,3}` would make it over-permissive, but the immediate fatal flaw is the space, which causes every well-formed IP address to fail the match. The correct regex contains no whitespace and repeats the octet pattern with a non-capturing group.
Trap 2: r'\d+\.\d+\.\d+\.\d+'
The `\d+` quantifier matches one or more digits with no upper bound, so this pattern incorrectly accepts octets like `1234` or `9999`, which are impossible in valid IPv4 dotted-decimal notation. While it does match well-formed addresses, its lack of a `{1,3}` length limit makes it too permissive for log parsing, where garbage strings such as `1234.5678.9012.3456` must be excluded. In contrast, the correct pattern constrains each octet to at most three digits.
- A
r'[0-9]+\. [0-9]+\.[0-9]+\.[0-9]+'
Why it fails: This pattern embeds a literal space after the first escaped dot (`\. `), so it expects input like `192. 168.1.1` rather than the standard `192.168.1.1` found in server logs. Even if the space were removed, using `+` instead of `{1,3}` would make it over-permissive, but the immediate fatal flaw is the space, which causes every well-formed IP address to fail the match. The correct regex contains no whitespace and repeats the octet pattern with a non-capturing group.
- B
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
This pattern uses `\d{1,3}` for each octet and is valid for matching IPv4 addresses, but it is longer and does not leverage a non-capturing group. While it works, the exam expects the more concise non-capturing group syntax in option C.
- C
r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}'
This pattern uses a non-capturing group `(?:...)` to repeat the first three octets (`[0-9]{1,3}\.`) exactly three times, followed by the final octet. It correctly matches dotted-decimal IP addresses without extra spaces or overly permissive digit counts, making it the most efficient and accurate choice.
- D
r'\d+\.\d+\.\d+\.\d+'
Why it fails: The `\d+` quantifier matches one or more digits with no upper bound, so this pattern incorrectly accepts octets like `1234` or `9999`, which are impossible in valid IPv4 dotted-decimal notation. While it does match well-formed addresses, its lack of a `{1,3}` length limit makes it too permissive for log parsing, where garbage strings such as `1234.5678.9012.3456` must be excluded. In contrast, the correct pattern constrains each octet to at most three digits.