|
2 | 2 |
|
3 | 3 | This comprehensive guide provides robust regular expressions for filtering barcodes based on common data patterns. These expressions are optimized to minimize false positives and false negatives. |
4 | 4 |
|
| 5 | +## 🔧 How to Combine Multiple Patterns |
| 6 | + |
| 7 | +### Understanding Pattern Combination |
| 8 | + |
| 9 | +When you need to match **multiple formats** with a single regex pattern, you can combine them using the **OR operator** (`|`). This allows you to create one pattern that matches any of several different formats. |
| 10 | + |
| 11 | +### Basic Syntax |
| 12 | + |
| 13 | +**Format**: `^(pattern1|pattern2|pattern3)$` |
| 14 | + |
| 15 | +**Explanation**: |
| 16 | +- `^` - Start of string |
| 17 | +- `(` - Begin group |
| 18 | +- `pattern1|pattern2|pattern3` - Match pattern1 OR pattern2 OR pattern3 |
| 19 | +- `)` - End group |
| 20 | +- `$` - End of string |
| 21 | + |
| 22 | +### Practical Example: Web Protocols |
| 23 | + |
| 24 | +Let's combine HTTP, HTTPS, and FTP patterns from this document: |
| 25 | + |
| 26 | +#### Individual Patterns: |
| 27 | +- **HTTP**: `^http://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(\:[0-9]{1,5})?(\/[^\s]*)?$` |
| 28 | +- **HTTPS**: `^https://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(\:[0-9]{1,5})?(\/[^\s]*)?$` |
| 29 | +- **FTP**: `^ftp://[a-zA-Z0-9.-]+(\:[0-9]{1,5})?(/[^\s]*)?$` |
| 30 | + |
| 31 | +#### Combined Pattern: |
| 32 | +```regex |
| 33 | +^(http://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(\:[0-9]{1,5})?(\/[^\s]*)?|https://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(\:[0-9]{1,5})?(\/[^\s]*)?|ftp://[a-zA-Z0-9.-]+(\:[0-9]{1,5})?(/[^\s]*)?)$ |
| 34 | +``` |
| 35 | + |
| 36 | +#### Simplified Alternative: |
| 37 | +For web protocols, you can use this more efficient pattern: |
| 38 | +```regex |
| 39 | +^(https?|ftp)://[a-zA-Z0-9.-]+(\:[0-9]{1,5})?(/[^\s]*)?$ |
| 40 | +``` |
| 41 | + |
| 42 | +**Explanation**: |
| 43 | +- `https?` - Matches "http" or "https" (the `?` makes the 's' optional) |
| 44 | +- `|ftp` - OR matches "ftp" |
| 45 | + |
| 46 | +### More Examples |
| 47 | + |
| 48 | +#### Phone Numbers (International + Local): |
| 49 | +```regex |
| 50 | +^(\+33[67][0-9]{8}|0[67][0-9]{8})$ |
| 51 | +``` |
| 52 | +**Matches**: `+33650203370` OR `0650203370` |
| 53 | + |
| 54 | +#### Product Codes (Multiple Formats): |
| 55 | +```regex |
| 56 | +^([0-9]{12}|[0-9]{13}|[0-9]{8})$ |
| 57 | +``` |
| 58 | +**Matches**: UPC-A (12 digits) OR EAN-13 (13 digits) OR EAN-8 (8 digits) |
| 59 | + |
| 60 | +#### Email + Phone: |
| 61 | +```regex |
| 62 | +^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|\+?[0-9\s\-\(\)\.]{10,15})$ |
| 63 | +``` |
| 64 | +**Matches**: Email addresses OR phone numbers |
| 65 | + |
| 66 | +### Quick Tips |
| 67 | + |
| 68 | +1. **Keep it Simple**: Start with 2-3 patterns, then expand as needed |
| 69 | +2. **Test Your Patterns**: Use online regex testers before implementing |
| 70 | +3. **Order Matters**: Put more specific patterns before general ones |
| 71 | +4. **Use Parentheses**: Always wrap your OR groups in parentheses |
| 72 | +5. **Common Prefixes**: Look for shared parts to simplify (like `https?` for HTTP/HTTPS) |
| 73 | + |
| 74 | +### Pattern Testing |
| 75 | + |
| 76 | +**Test your combined patterns with these examples**: |
| 77 | +- ✅ `http://example.com` - Should match |
| 78 | +- ✅ `https://secure.example.com:443/path` - Should match |
| 79 | +- ✅ `ftp://files.example.com/download` - Should match |
| 80 | +- ❌ `invalid://bad.url` - Should NOT match |
| 81 | + |
5 | 82 | ## 📋 Table of Contents |
6 | 83 |
|
7 | 84 | ### 🌐 [Digital & Web Identifiers](#-digital--web-identifiers) |
|
0 commit comments