@@ -50,6 +50,120 @@ separated from surrounding elements by spaces. Note that items listed in
5050| is \[ not\] odd by | | $a is not odd by $b | \[ not\] an odd grouping | ($a / $b) % 2 != 0 |
5151| is in | | $a is in $b | exists in array | in_array($a, $b) |
5252| is \[ not\] in | | $a is not in $b | does not exist in array | !in_array($a, $b) |
53+ | matches | | $a matches $b | regex pattern match | preg_match($b, $a) |
54+
55+ ## Regex Matching Operator
56+
57+ The ` matches ` operator allows you to test if a string matches a regular expression pattern.
58+
59+ ### Basic Usage
60+
61+ ``` smarty
62+ {if "hello" matches "/^[a-z]+$/"}
63+ String matches the pattern!
64+ {/if}
65+
66+ {if $email matches "/^[^@]+@[^@]+\.[^@]+$/"}
67+ Valid email format
68+ {else}
69+ Invalid email format
70+ {/if}
71+ ```
72+
73+ ### Using Variables
74+
75+ ``` smarty
76+ {$pattern = '/^[a-zA-Z0-9]{8,}$/'}
77+ {if $password matches $pattern}
78+ Password meets requirements
79+ {else}
80+ Password must be at least 8 alphanumeric characters
81+ {/if}
82+ ```
83+
84+ ### Pattern Modifiers
85+
86+ The ` matches ` operator supports all standard PHP regex modifiers:
87+
88+ ``` smarty
89+ {* Case insensitive matching *}
90+ {if "HELLO" matches "/hello/i"}
91+ Matches (case insensitive)
92+ {/if}
93+
94+ {* Multiline mode *}
95+ {if "line1\nline2" matches "/line2$/m"}
96+ Matches in multiline mode
97+ {/if}
98+
99+ {* Dot matches newlines *}
100+ {if "hello\nworld" matches "/hello.world/s"}
101+ Matches with dotall modifier
102+ {/if}
103+ ```
104+
105+ ### Complex Conditions
106+
107+ The ` matches ` operator can be combined with other operators:
108+
109+ ``` smarty
110+ {if $username matches "/^[a-z]+$/" && $username|length > 3}
111+ Valid username
112+ {else}
113+ Username must be lowercase letters and at least 4 characters
114+ {/if}
115+
116+ {if $input matches "/^[0-9]+$/" || $input matches "/^[a-z]+$/"}
117+ Input is either numeric or lowercase letters
118+ {else}
119+ Invalid input format
120+ {/if}
121+ ```
122+
123+ ### Practical Examples
124+
125+ ** Email Validation:**
126+ ``` smarty
127+ {if $email matches "/^[^@\s]+@[^@\s]+\.[^@\s]+$/"}
128+ Valid email address
129+ {else}
130+ Please enter a valid email address
131+ {/if}
132+ ```
133+
134+ ** Password Strength:**
135+ ``` smarty
136+ {if $password matches "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/"}
137+ Strong password
138+ {else}
139+ Password must contain uppercase, lowercase, numbers and be at least 8 characters
140+ {/if}
141+ ```
142+
143+ ** URL Validation:**
144+ ``` smarty
145+ {if $url matches "/^https?:\/\/(www\.)?[a-z0-9\-]+(\.[a-z]{2,})+/i"}
146+ Valid URL format
147+ {else}
148+ Please enter a valid URL
149+ {/if}
150+ ```
151+
152+ ** Numeric Validation:**
153+ ``` smarty
154+ {if $input matches "/^[0-9]+$/"}
155+ Valid numeric input
156+ {else}
157+ Please enter numbers only
158+ {/if}
159+ ```
160+
161+ ### Notes
162+
163+ - The ` matches ` operator uses PHP's ` preg_match() ` function internally
164+ - Pattern delimiters must be valid regex delimiters (typically ` / ` )
165+ - Invalid patterns will cause PHP warnings but won't break template execution
166+ - For complex regex patterns, consider using variables for better readability
53167
54168## Ternary
55169You can use the ` ?: ` (or ternary) operator to test one expression and present the value
0 commit comments