String pattern replacement · replace

🟢 6 of 6 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["String pattern replacement · replace<br/>🟢6"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_two["replace(DS, pattern1, pattern2)"]:::ok
        v_ds_one["replace(DS, pattern1)"]:::ok
        v_ds_one_placeholder["replace(DS, pattern1, _)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_two["replace(comp, pattern1, pattern2)"]:::ok
        v_comp_patterns["replace(comp, comp, comp)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_two["replace(sc, pattern1, pattern2)"]:::ok
    end
    O --- G_ds
    O --- G_comp
    O --- G_sc
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
    click v_ds_two href "#form-ds-two"
    click v_ds_one href "#form-ds-one"
    click v_ds_one_placeholder href "#form-ds-one-placeholder"
    click v_comp_two href "#form-comp-two"
    click v_comp_patterns href "#form-comp-patterns"
    click v_sc_two href "#form-sc-two"
    

Types

Takes string · Gives string

Comes from · string — 12 operators in 3 blocks 🟢12
General purpose operators (2) 🟢2
String operators (7) 🟢7
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 37 operators in 8 blocks 🟢37
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Time operators (3) 🟢3
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Goes to · string — 16 operators in 5 blocks 🟢16
General purpose operators (1) 🟢1
String operators (10) 🟢10
Comparison operators (1) 🟢1
Data validation operators (1) 🟢1
Definitions (VTL-DL) (3) 🟢3
Goes to · operators that take any scalar — 49 operators in 9 blocks 🟢49
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Definitions (VTL-DL) (1) 🟢1

Forms

replace(DS, pattern1, pattern2) 🟢 · string → string

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

replace(DS_1, "ello", "i"): ello replaced by i in every value.

Manual:

String pattern replacement, Example 1, output verbatim: “Replaces all the occurrences of a specified string-pattern (pattern1) with another one (pattern2).”

Expected:

hello world, say hello and hello! give hi world, say hi and hi!; he has no ello and is kept.

script.vtl
DS_r := replace (DS_1,"ello","i");
Input DS_1
Id_1Id_2Me_1
1Ahello world
2Asay hello
3Ahe
4Ahello!
Expected DS_r
Id_1Id_2Me_1
1Ahi world
2Asay hi
3Ahe
4Ahi!

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

replace(DS, pattern1) 🟢 · string → string

Replacement omitted 🟢 pandas ✅ · DuckDB ✅
Tests:

replace(DS_1, "l") with pattern2 omitted.

Manual:

String pattern replacement: “If pattern2 is omitted then all occurrences of pattern1 are removed.”

Expected:

hello world, which has three l, gives heo word; say has none and is kept. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := replace(DS_1, "l");
Input DS_1
Id_1Me_1
1hello world
2say
3
Expected DS_r
Id_1Me_1
1heo word
2say
3

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

replace(DS, pattern1, _) 🟢 · string → string

Placeholder for replacement 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for pattern2: replace(DS_1, "a", _) is replace(DS_1, "a") with the underscore written out.

Manual:

In the invocation, a non-specified optional operand “is substituted by an underscore or, if it is in the end of the invocation, can be omitted” (Overview and conventions, syntax conventions); String pattern replacement: “If pattern2 is omitted then all occurrences of pattern1 are removed.”

Expected:

banana, which has three a, gives bnn; xyz has none and is kept. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := replace(DS_1, "a", _);
Input DS_1
Id_1Me_1
1banana
2xyz
3
Expected DS_r
Id_1Me_1
1bnn
2xyz
3

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

replace(comp, pattern1, pattern2) 🟢 · string → string

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

replace(Me_1, "ello", "i") inside calc, where the new measure Me_2 takes the result.

Manual:

String pattern replacement, Example 2, output verbatim: “Replaces all the occurrences of a specified string-pattern (pattern1) with another one (pattern2).”

Expected:

Me_2 is hi world, say hi, he and hi!; Me_1 is kept.

script.vtl
DS_r := DS_1[ calc Me_2:= replace (Me_1,"ello","i")];
Input DS_1
Id_1Id_2Me_1
1Ahello world
2Asay hello
3Ahe
4Ahello!
Expected DS_r
Id_1Id_2Me_1Me_2
1Ahello worldhi world
2Asay hellosay hi
3Ahehe
4Ahello!hi!

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

replace(comp, comp, comp) 🟢 · string → string

Patterns from components 🟢 pandas ✅ · DuckDB ✅
Tests:

replace(Me_1, Me_2, Me_3) inside calc: both patterns come from components, a different pair for each data point.

Manual:

String pattern replacement types pattern1 and pattern2 as component<string> | string, and “Replaces all the occurrences of a specified string-pattern (pattern1) with another one (pattern2).”

Expected:

Me_4 is hello VTL, bANANa and abc: both an of banana are replaced, and x does not occur in abc. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := DS_1[calc Me_4 := replace(Me_1, Me_2, Me_3)];
Input DS_1
Id_1Me_1Me_2Me_3
1hello worldworldVTL
2bananaanAN
3abcxy
4ab
Expected DS_r
Id_1Me_1Me_2Me_3Me_4
1hello worldworldVTLhello VTL
2bananaanANbANANa
3abcxyabc
4ab

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

replace(sc, pattern1, pattern2) 🟢 · string → string

Leading word replaced 🟢 pandas ✅ · DuckDB ✅
Tests:

replace(SC_1, SC_2, SC_3) with SC_1 = Hello world, SC_2 = Hello and SC_3 = Hi, where the pattern is the first word.

Manual:

String pattern replacement: “Replaces all the occurrences of a specified string-pattern (pattern1) with another one (pattern2).”, as in replace("Hello world", "Hello", "Hi") gives "Hi world".

Expected:

SC_r is Hi world, a String.

script.vtl
SC_r := replace(SC_1, SC_2, SC_3);
Input scalars
SC_1SC_2SC_3
Hello worldHelloHi
Expected scalars

Name

Type

Value

SC_r

String

"Hi world"

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗