Skip to content

PHPLIB-1694 Support regular expressions in $replaceAll search string and $split delimiter #1739

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

Merged
merged 1 commit into from
Aug 4, 2025
Merged
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
11 changes: 11 additions & 0 deletions generator/config/expression/replaceAll.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ arguments:
type:
- resolvesToString
- resolvesToNull
- resolvesToRegex
description: |
The string to search for within the given input. Can be any valid expression that resolves to a string or a null. If find refers to a field that is missing, $replaceAll returns null.
-
Expand All @@ -42,3 +43,13 @@ tests:
input: '$item'
find: 'blue paint'
replacement: 'red paint'
-
name: 'Support regex search string'
pipeline:
-
$project:
item:
$replaceAll:
input: '123-456-7890'
find: !bson_regex '\d{3}'
replacement: 'xxx'
10 changes: 10 additions & 0 deletions generator/config/expression/split.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ arguments:
name: delimiter
type:
- resolvesToString
- resolvesToRegex
description: |
The delimiter to use when splitting the string expression. delimiter can be any valid expression as long as it resolves to a string.
tests:
Expand Down Expand Up @@ -46,3 +47,12 @@ tests:
-
$sort:
total_qty: -1
-
name: 'Support regex delimiter'
pipeline:
-
$project:
split:
$split:
- 'abc'
- !bson_regex 'b'
12 changes: 7 additions & 5 deletions src/Builder/Expression/FactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/Builder/Expression/ReplaceAllOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/Builder/Expression/SplitOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions tests/Builder/Expression/Pipelines.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/Builder/Expression/ReplaceAllOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MongoDB\Tests\Builder\Expression;

use MongoDB\BSON\Regex;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
Expand All @@ -28,4 +29,19 @@ public function testExample(): void

$this->assertSamePipeline(Pipelines::ReplaceAllExample, $pipeline);
}

public function testSupportRegexSearchString(): void
{
$pipeline = new Pipeline(
Stage::project(
item: Expression::replaceAll(
input: '123-456-7890',
find: new Regex('\d{3}'),
replacement: 'xxx',
),
),
);

$this->assertSamePipeline(Pipelines::ReplaceAllSupportRegexSearchString, $pipeline);
}
}
14 changes: 14 additions & 0 deletions tests/Builder/Expression/SplitOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ public function testExample(): void

$this->assertSamePipeline(Pipelines::SplitExample, $pipeline);
}

public function testSupportRegexDelimiter(): void
{
$pipeline = new Pipeline(
Stage::project(
split: Expression::split(
string: 'abc',
delimiter: new Regex('b'),
),
),
);

$this->assertSamePipeline(Pipelines::SplitSupportRegexDelimiter, $pipeline);
}
}