紀錄以後也許會用到。Matching an IP address is another good example of a trade-off between regex complexity and exactness. d{1,3}.d{1,3}.d{1,3}.d{1,3} will match any IP address just fine, but will also match 999.999.999.999 as if it were a valid IP address. Whether this is a problem depends on the files or data you intend to apply the regex to. To restrict all 4 numbers in the IP address to 0..255, you can use this complex beast: (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]?) Analyze this regular expression with RegexBuddy (everything on a single line). The long regex stores each of the 4 numbers of the IP address into a capturing group. You can use these groups to further process the IP number.
If you don't need access to the individual numbers, you can shorten the regex with a quantifier to: (?:(?: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]?) Analyze this regular expression with RegexBuddy. Similarly, you can shorten the quick regex to (?:d{1,3}.){3}d{1,3} Analyze this regular expression with RegexBuddy
If you don't need access to the individual numbers, you can shorten the regex with a quantifier to: (?:(?: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]?) Analyze this regular expression with RegexBuddy. Similarly, you can shorten the quick regex to (?:d{1,3}.){3}d{1,3} Analyze this regular expression with RegexBuddy


請先 登入 以發表留言。