The company is working on a small national encryption gateway project and does not want to use a computer-based host. It needs to support browser access. The gateway configuration involves parameters such as IP address and subnet mask, as shown in the figure below. To meet the robustness requirements of the program, it is necessary to validate the legality of the IP address on both the browser HTML side and the board-side C program. Regular expression functions are used in the board-side C program to achieve this requirement.
There are four regular expression functions in Linux: regcomp, regexec, regerror, and regfree. You can execute man 3 regex and man 7 regex in the Linux distribution command line to view detailed information.
Below is an example program generated by DeepSeek along with its execution results,
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <regex.h>
/** * Validate if the IP address is legal * @param ip_str The IP address string to validate * @return Returns 0 if legal, 1 if illegal */int validate_ip_address(const char *ip_str) { regex_t regex; int ret; char msgbuf[100];
// Regular expression pattern for IP address // Explanation: ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ // - 25[0-5]: Matches 250-255 // - 2[0-4][0-9]: Matches 200-249 // - [01]?[0-9][0-9]?: Matches 0-199 (leading zeros are allowed, but technically an IP address part can have leading zeros) // - {3}: The preceding group repeats 3 times (i.e., 3 numbers plus 3 dots) // - Finally matches one number (without a dot) const char *pattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
// Compile the regular expression ret = regcomp(®ex, pattern, REG_EXTENDED); if (ret != 0) { regerror(ret, ®ex, msgbuf, sizeof(msgbuf)); fprintf(stderr, "Could not compile regex: %s\n", msgbuf); regfree(®ex); return 1; // Compilation failure is also considered illegal }
// Execute the match ret = regexec(®ex, ip_str, 0, NULL, 0);
// Free the regular expression resources regfree(®ex);
if (ret == 0) { return 0; // Match successful, IP address is legal } else if (ret == REG_NOMATCH) { return 1; // No match, IP address is illegal } else { // Other errors regerror(ret, ®ex, msgbuf, sizeof(msgbuf)); fprintf(stderr, "Regex match failed: %s\n", msgbuf); return 1; // Execution error is also considered illegal }}
// Test functionint main() { const char *test_ips[] = { "192.168.1.1", "255.255.255.255", "0.0.0.0", "127.0.0.1", "256.1.1.1", // Illegal: 256 > 255 "192.168.1", // Illegal: only 3 segments "192.168.1.1.1", // Illegal: 5 segments "192.168.1.x", // Illegal: contains non-numeric characters "192.168.01.1", // Legal: has leading zero (technically allowed, but usually not recommended) "1.2.3.4", "999.999.999.999", // Illegal NULL };
printf("Testing IP address validation:\n"); printf("==============================\n");
for (int i = 0; test_ips[i] != NULL; i++) { int result = validate_ip_address(test_ips[i]); printf("%-15s -> %s\n", test_ips[i], result == 0 ? "Valid" : "Invalid"); }
// Interactive testing printf("\nEnter an IP address to validate (or 'quit' to exit):\n"); char input[100];
while (1) { printf("> "); if (fgets(input, sizeof(input), stdin) == NULL) { break; }
// Remove newline character input[strcspn(input, "\n")] = '\0';
if (strcmp(input, "quit") == 0 || strcmp(input, "exit") == 0) { break; }
if (strlen(input) == 0) { continue; }
int result = validate_ip_address(input); printf("Result: %s\n\n", result == 0 ? "Valid IP" : "Invalid IP"); }
return 0;}

There is also a JavaScript function for validating IP parameters on the HTML side, as shown in the following code,
/*** Simple IP format validation*/function validateIP(ip) { const ipPattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; return ipPattern.test(ip);}
// Basic front-end validationif (!validateIP(config.wan.ip)) { showError(statusMsg, 'WAN IP address format is invalid'); return;}