Home >Backend Development >PHP Tutorial >How to Fix the 'Delimiter must not be alphanumeric or backslash' Error in PHP's `preg_match`?
When encountering the error "Delimiter must not be alphanumeric or backslash," it indicates a missing delimiter in your regular expression.
The provided code attempts to extract the name between single quotes from a string using preg_match. However, the regular expression requires a delimiter to define the boundaries of the pattern.
Solution
Add a delimiter to the regular expression. Commonly used delimiters include forward slash (/), pound (#), and backtick (`). In this case, we'll use a forward slash:
$pattern = "/My name is '(.*)' and im fine/";
By enclosing the pattern with forward slashes, we properly define the start and end of the pattern, preventing the error you encountered.
Updated Code
$string1 = "My name is 'Kate' and im fine"; $pattern = "/My name is '(.*)' and im fine/"; preg_match($pattern, $string1, $matches); echo $matches[1]; // Output: Kate
This updated code will successfully extract and display the name enclosed in single quotes. Remember, when defining a regular expression pattern, it's essential to include a valid delimiter to avoid parsing errors.
The above is the detailed content of How to Fix the 'Delimiter must not be alphanumeric or backslash' Error in PHP's `preg_match`?. For more information, please follow other related articles on the PHP Chinese website!