String pattern replacement · replace
---
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
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"):elloreplaced byiin 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 helloandhello!givehi world,say hiandhi!;hehas noelloand is kept.
DS_r := replace (DS_1,"ello","i");
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | hello world |
| 2 | A | say hello |
| 3 | A | he |
| 4 | A | hello! |
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | hi world |
| 2 | A | say hi |
| 3 | A | he |
| 4 | A | hi! |
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")withpattern2omitted.- Manual:
String pattern replacement: “If
pattern2is omitted then all occurrences ofpattern1are removed.”- Expected:
hello world, which has threel, givesheo word;sayhas 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’slengthandinstrexamples show.
DS_r := replace(DS_1, "l");
| Id_1 | Me_1 |
|---|---|
| 1 | hello world |
| 2 | say |
| 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | heo word |
| 2 | say |
| 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
_forpattern2:replace(DS_1, "a", _)isreplace(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
pattern2is omitted then all occurrences ofpattern1are removed.”- Expected:
banana, which has threea, givesbnn;xyzhas 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’slengthandinstrexamples show.
DS_r := replace(DS_1, "a", _);
| Id_1 | Me_1 |
|---|---|
| 1 | banana |
| 2 | xyz |
| 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | bnn |
| 2 | xyz |
| 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")insidecalc, where the new measureMe_2takes 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_2ishi world,say hi,heandhi!;Me_1is kept.
DS_r := DS_1[ calc Me_2:= replace (Me_1,"ello","i")];
| Id_1 | Id_2 | Me_1 |
|---|---|---|
| 1 | A | hello world |
| 2 | A | say hello |
| 3 | A | he |
| 4 | A | hello! |
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | hello world | hi world |
| 2 | A | say hello | say hi |
| 3 | A | he | he |
| 4 | A | hello! | 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)insidecalc: both patterns come from components, a different pair for each data point.- Manual:
String pattern replacement types
pattern1andpattern2ascomponent<string> | string, and “Replaces all the occurrences of a specified string-pattern (pattern1) with another one (pattern2).”- Expected:
Me_4ishello VTL,bANANaandabc: bothanofbananaare replaced, andxdoes not occur inabc. 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’slengthandinstrexamples show.
DS_r := DS_1[calc Me_4 := replace(Me_1, Me_2, Me_3)];
| Id_1 | Me_1 | Me_2 | Me_3 |
|---|---|---|---|
| 1 | hello world | world | VTL |
| 2 | banana | an | AN |
| 3 | abc | x | y |
| 4 | a | b |
| Id_1 | Me_1 | Me_2 | Me_3 | Me_4 |
|---|---|---|---|---|
| 1 | hello world | world | VTL | hello VTL |
| 2 | banana | an | AN | bANANa |
| 3 | abc | x | y | abc |
| 4 | a | b |
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)withSC_1=Hello world,SC_2=HelloandSC_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 inreplace("Hello world", "Hello", "Hi")gives"Hi world".- Expected:
SC_risHi world, a String.
SC_r := replace(SC_1, SC_2, SC_3);
| SC_1 | SC_2 | SC_3 |
|---|---|---|
| Hello world | Hello | Hi |
Name |
Type |
Value |
|---|---|---|
|
String |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗