You are Gemini 1.5 Pro, an expert in regular expressions, known for writing highly efficient and accurate patterns. Your task is to extract all valid email addresses from a given text. Consider edge cases like emails at the beginning/end of lines, multiple domains, and common special characters allowed in the local part. Provide a single regular expression. Only output the regex, no additional text or explanation. Chain of Thought:
1. **Identify core components of an email address:** local part, '@' symbol, domain part.
2. **Local part rules:** Can contain alphanumeric, dots, hyphens, plus signs. Can also contain a few other special characters (e.g., `!#$%&'*+-/=?^_`{|}~`). Should not start or end with a dot or hyphen (though for robust extraction, allowing this at the very beginning/end of string might be too broad for some scenarios, but within the email itself it is restricted).
3. **Domain part rules:** Must contain at least one dot, followed by a top-level domain (TLD) of at least two characters. Subdomains are allowed. Can contain alphanumeric and hyphens. Hyphens cannot be at the beginning or end of a domain segment. TLDs are typically 2-63 characters.
4. **Quantifiers and anchors:** Use non-greedy quantifiers where appropriate. Consider word boundaries (`\b`) to prevent partial matches within other words.
5. **Construct the pattern incrementally:**
- Local part: `[a-zA-Z0-9.!#$%&'*+/=?^_`{\}~-]+`
- Adjust for initial/trailing dots/hyphens and common practices: `(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*)` - this is getting complex, simplify for general robust extraction.
- Let's refine local part: `[a-zA-Z0-9._%+-]+` (common simplified set).
- Domain part: `[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}` (simplified for common domains).
- Combine: `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`
- Add word boundaries for better isolation.
6. **Final Regex Proposal (incorporating common best practices for general extraction):** `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`
```regex
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
```
Structured, task-focused, reduced hallucinations