import requests
import re
response = requests.get('https://www.ip2location.com/')
html_content = response.text
# Regular expression to find IP addresses
ip_addresses = re.findall(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b', html_content)
print("Extracted IP Addresses:", ip_addresses)
### Explanation of the Regular Expression: [A-Za-z]{5,7}
#### Pattern Breakdown:
1. **Character Class: `[A-Za-z]`**
- This specifies a range of characters to match.
- Matches **any uppercase letter (A-Z)** or **lowercase letter (a-z)**.
- Examples:
- Matches: `A`, `b`, `C`, `z`
- Does not match: `1`, `@`, `space`.
2. **Quantifier: `{5,7}`**
- Specifies that the preceding character class (`[A-Za-z]`) must appear at least **5 times** and at most **7 times**.
- Examples:
- Matches: `hello` (5 characters), `worlds` (6 characters), `testing` (7 characters)
- Does not match: `abc` (3 characters, too short), `abcdefgh` (8 characters, too long).
3. **Combining Them: `[A-Za-z]{5,7}`**
- Matches any string containing **only letters** (no spaces, numbers, or symbols) with a length of 5 to 7.
#### Matching Criteria:
- **Matches Strings That:**
- Contain only uppercase and/or lowercase letters.
- Have a length of at least 5 characters and no more than 7.
- **Does Not Match Strings That:**
- Are shorter than 5 characters (e.g., `abc`).
- Are longer than 7 characters (e.g., `abcdefgh`).
- Contain non-letter characters (e.g., `ab12`, `hello!`).
#### Examples:
1. **Matches:**
- `"hello"` → 5 characters, only letters.
- `"worlds"` → 6 characters, only letters.
- `"abcdef"` → 6 characters, only letters.
- `"testing"` → 7 characters, only letters.
2. **Does Not Match:**
- `"abc"` → Only 3 characters (too short).
- `"abcdefgh"` → 8 characters (too long).
- `"ab12"` → Contains non-letters (`1` and `2`).
- `" "` → Contains a space (not a letter).
#### Adjusting the Pattern:
- **To match shorter strings (e.g., `abc`):**
- Use `[A-Za-z]{3,7}` to match between 3 and 7 characters.
- **To match exactly 3 characters:**
- Use `[A-Za-z]{3}`.
#### Summary:
The pattern `[A-Za-z]{5,7}` ensures that only strings with 5 to 7 alphabetic characters are matched. It's a powerful way to validate lengths and enforce content rules when working with text.
0 Comments