Laravel preg_match(): No ending delimiter '/' found

PhpRegexLaravelLaravel 4Regexp Replace

Php Problem Overview


Im working on Laravel 4.2. Im trying to use Validator to validate a name field with regex, here is my rule below:

 public static $rules_save = [
		
		'class_subjects' => 'required|regex:/[0-9]([0-9]|-(?!-))+/'
	];

But as soon as I call the rule to be validated an error is thrown, see below:

preg_match(): No ending delimiter '/' found

Php Solutions


Solution 1 - Php

Since your regex has a pipe in it, you have to use an array:

public static $rules_save = [
    'class_subjects' => ['required', 'regex:/[0-9]([0-9]|-(?!-))+/'],
];

From the docs:

> When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArlindView Question on Stackoverflow
Solution 1 - PhpJoseph SilberView Answer on Stackoverflow