Skip to content

Commit 5de0a58

Browse files
authored
Support regular expressions in $replaceAll search string and $split delimiter (#1739)
1 parent eac625a commit 5de0a58

File tree

8 files changed

+114
-14
lines changed

8 files changed

+114
-14
lines changed

generator/config/expression/replaceAll.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ arguments:
2121
type:
2222
- resolvesToString
2323
- resolvesToNull
24+
- resolvesToRegex
2425
description: |
2526
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.
2627
-
@@ -42,3 +43,13 @@ tests:
4243
input: '$item'
4344
find: 'blue paint'
4445
replacement: 'red paint'
46+
-
47+
name: 'Support regex search string'
48+
pipeline:
49+
-
50+
$project:
51+
item:
52+
$replaceAll:
53+
input: '123-456-7890'
54+
find: !bson_regex '\d{3}'
55+
replacement: 'xxx'

generator/config/expression/split.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ arguments:
1717
name: delimiter
1818
type:
1919
- resolvesToString
20+
- resolvesToRegex
2021
description: |
2122
The delimiter to use when splitting the string expression. delimiter can be any valid expression as long as it resolves to a string.
2223
tests:
@@ -46,3 +47,12 @@ tests:
4647
-
4748
$sort:
4849
total_qty: -1
50+
-
51+
name: 'Support regex delimiter'
52+
pipeline:
53+
-
54+
$project:
55+
split:
56+
$split:
57+
- 'abc'
58+
- !bson_regex 'b'

src/Builder/Expression/FactoryTrait.php

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/ReplaceAllOperator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/SplitOperator.php

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/Expression/Pipelines.php

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/Expression/ReplaceAllOperatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MongoDB\Tests\Builder\Expression;
66

7+
use MongoDB\BSON\Regex;
78
use MongoDB\Builder\Expression;
89
use MongoDB\Builder\Pipeline;
910
use MongoDB\Builder\Stage;
@@ -28,4 +29,19 @@ public function testExample(): void
2829

2930
$this->assertSamePipeline(Pipelines::ReplaceAllExample, $pipeline);
3031
}
32+
33+
public function testSupportRegexSearchString(): void
34+
{
35+
$pipeline = new Pipeline(
36+
Stage::project(
37+
item: Expression::replaceAll(
38+
input: '123-456-7890',
39+
find: new Regex('\d{3}'),
40+
replacement: 'xxx',
41+
),
42+
),
43+
);
44+
45+
$this->assertSamePipeline(Pipelines::ReplaceAllSupportRegexSearchString, $pipeline);
46+
}
3147
}

tests/Builder/Expression/SplitOperatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,18 @@ public function testExample(): void
5050

5151
$this->assertSamePipeline(Pipelines::SplitExample, $pipeline);
5252
}
53+
54+
public function testSupportRegexDelimiter(): void
55+
{
56+
$pipeline = new Pipeline(
57+
Stage::project(
58+
split: Expression::split(
59+
string: 'abc',
60+
delimiter: new Regex('b'),
61+
),
62+
),
63+
);
64+
65+
$this->assertSamePipeline(Pipelines::SplitSupportRegexDelimiter, $pipeline);
66+
}
5367
}

0 commit comments

Comments
 (0)