200-201 · Question #196
Which regular expression is needed to capture the IP address 192.168.20.232?
The correct answer is A. ^ (?:[0-9]{1,3}\.){3}[0-9]{1,3}. The correct regular expression to capture a standard IPv4 address requires matching four octets, each consisting of 1 to 3 digits, separated by literal dots.
Question
Which regular expression is needed to capture the IP address 192.168.20.232?
Options
- A^ (?:[0-9]{1,3}.){3}[0-9]{1,3}
- B^ (?:[0-9]f1,3}.){1,4}
- C^ (?:[0-9]{1,3}.)'
- D^ ([0-9]-{3})
How the community answered
(22 responses)- A86% (19)
- B9% (2)
- D5% (1)
Why each option
The correct regular expression to capture a standard IPv4 address requires matching four octets, each consisting of 1 to 3 digits, separated by literal dots.
The regex `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}` accurately captures an IPv4 address by matching three occurrences of an octet (1-3 digits) followed by a dot, and then a final octet. The `^` anchor ensures the match starts at the beginning of the string, while `(?:...)` creates a non-capturing group for the repeating octet pattern.
This regex allows for 1 to 4 repeated octets, which would match invalid IP address formats that do not have exactly four octets.
This regex is incomplete and syntactically incorrect, missing proper closure for its grouping and failing to specify the full four-octet structure of an IP address.
This regex uses incorrect syntax `([0-9]-{{3}})` and does not correctly represent the structure of an IPv4 address with four dot-separated octets.
Concept tested: Regular expression for IPv4 addresses
Source: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Topics
Community Discussion
No community discussion yet for this question.