Skip to content

[12.x] Add isRegex stringable helper #56561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,25 @@ public static function isJson($value)
return true;
}

/**
* Determine if a given value is a valid regex.
*
* @param mixed $value
* @return bool
*/
public static function isRegex($value)
{
if (! is_string($value)) {
return false;
}

if ($value === '') {
return false;
}

return @preg_match($value, '') !== false;
}

/**
* Determine if a given value is a valid URL.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ public function isJson()
return Str::isJson($this->value);
}

/**
* Determine if a given string is valid Regex.
*
* @return bool
*/
public function isRegex()
{
return Str::isRegex($this->value);
}

/**
* Determine if a given value is a valid URL.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ public function testIsJson()
$this->assertFalse($this->stringable(null)->isJson());
}

public function testIsRegex()
{
// Test valid regex patterns
$this->assertTrue($this->stringable('/hello/')->isRegex());
$this->assertTrue($this->stringable('/hello/i')->isRegex());
$this->assertTrue($this->stringable('/^[a-z]+$/')->isRegex());
$this->assertTrue($this->stringable('/\d+/')->isRegex());
$this->assertTrue($this->stringable('~hello world~')->isRegex());
$this->assertTrue($this->stringable('@hello@i')->isRegex());
$this->assertTrue($this->stringable('#[a-zA-Z0-9]#')->isRegex());
$this->assertTrue($this->stringable('![a-z]+!u')->isRegex());
$this->assertTrue($this->stringable('%\s+%m')->isRegex());

// Test complex valid patterns
$this->assertTrue($this->stringable('/(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})/')->isRegex());
$this->assertTrue($this->stringable('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/')->isRegex());
$this->assertTrue($this->stringable('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/')->isRegex());

// Test invalid regex patterns
$this->assertFalse($this->stringable('/hello')->isRegex()); // Missing closing delimiter
$this->assertFalse($this->stringable('hello/')->isRegex()); // Missing opening delimiter
$this->assertFalse($this->stringable('/[a-z+/')->isRegex()); // Unclosed bracket
$this->assertFalse($this->stringable('/(/')->isRegex()); // Unclosed parenthesis
$this->assertFalse($this->stringable('/(?P<name>test/')->isRegex()); // Unclosed named group
$this->assertFalse($this->stringable()->isRegex()); // Empty string

// Test plain strings (not regex patterns)
$this->assertFalse($this->stringable('hello world')->isRegex());
$this->assertFalse($this->stringable('123')->isRegex());
$this->assertFalse($this->stringable('simple text')->isRegex());

// Test edge cases
$this->assertTrue($this->stringable('/./')->isRegex()); // Dot pattern
$this->assertTrue($this->stringable('/$/')->isRegex()); // End anchor
$this->assertTrue($this->stringable('/^$/')->isRegex()); // Empty line pattern
}

public function testIsMatch()
{
$this->assertTrue($this->stringable('Hello, Laravel!')->isMatch('/.*,.*!/'));
Expand Down
Loading