Skip to content

Commit cccc06f

Browse files
thejudanjakefala
andauthored
[guide] Add a RegexGuide (#2552)
Co-authored-by: anjakefala <[email protected]>
1 parent a743607 commit cccc06f

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

visidata/features/regex.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ def inputRegexSubst(vd, prompt):
126126
after=dict(type='regex-replace', prompt='replace: ', help=prompt))
127127

128128

129-
Sheet.addCommand(':', 'addcol-split', 'addColumnAtCursor(RegexColumn(makeRegexSplitter, cursorCol, inputRegex("split regex: ", type="regex-split")))', 'Add column split by regex')
130-
Sheet.addCommand(';', 'addcol-capture', 'addColumnAtCursor(RegexColumn(makeRegexMatcher, cursorCol, inputRegex("capture regex: ", type="regex-capture")))', 'Add column captured by regex')
129+
Sheet.addCommand(':', 'addcol-split', 'addColumnAtCursor(RegexColumn(makeRegexSplitter, cursorCol, inputRegex("split regex: ", type="regex-split")))', 'add column split by regex')
130+
Sheet.addCommand(';', 'addcol-capture', 'addColumnAtCursor(RegexColumn(makeRegexMatcher, cursorCol, inputRegex("capture regex: ", type="regex-capture")))', 'add column captured by regex')
131131

132-
Sheet.addCommand('*', 'addcol-regex-subst', 'addColumnAtCursor(Column(cursorCol.name + "_re", getter=regexTransform(cursorCol, **inputRegexSubst("regex transform column"))))', 'add column derived from current column, replacing regex with subst (may include \1 backrefs)')
133-
Sheet.addCommand('g*', 'setcol-regex-subst', 'setValuesFromRegex([cursorCol], someSelectedRows, **inputRegexSubst("regex transform column"))', 'regex/subst - modify selected rows in current column, replacing regex with subst, (may include backreferences \\1 etc)')
134-
Sheet.addCommand('gz*', 'setcol-regex-subst-all', 'setValuesFromRegex(visibleCols, someSelectedRows, **inputRegexSubst(f"regex transform {nVisibleCols} columns"))', 'modify selected rows in all visible columns, replacing regex with subst (may include \\1 backrefs)')
132+
Sheet.addCommand('*', 'addcol-regex-subst', 'addColumnAtCursor(Column(cursorCol.name + "_re", getter=regexTransform(cursorCol, **inputRegexSubst("regex transform column"))))', 'add column derived from current column, replacing `search` regex with `replace` (may include \\1 backrefs)')
133+
Sheet.addCommand('g*', 'setcol-regex-subst', 'setValuesFromRegex([cursorCol], someSelectedRows, **inputRegexSubst("regex transform column"))', 'modify selected rows in current column, replacing `search` regex with `replace`, (may include backreferences \\1 etc)')
134+
Sheet.addCommand('gz*', 'setcol-regex-subst-all', 'setValuesFromRegex(visibleCols, someSelectedRows, **inputRegexSubst(f"regex transform {nVisibleCols} columns"))', 'modify selected rows in all visible columns, replacing `search` regex with `replace` (may include \\1 backrefs)')
135135

136136

137137
vd.addMenuItems('''

visidata/guides/RegexGuide.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
sheet: Sheet
3+
---
4+
# Matching and Transforming Strings with Regex
5+
6+
Some commands for selecting, searching, and transforming data, accept a regular expression as input.
7+
8+
## Select Rows
9+
10+
- {help.commands.select-col-regex}
11+
- {help.commands.select-cols-regex}
12+
13+
- {help.commands.unselect-col-regex}
14+
- {help.commands.unselect-cols-regex}
15+
16+
## Search
17+
18+
- {help.commands.go-col-regex}
19+
20+
- {help.commands.search-col}
21+
- {help.commands.search-cols}
22+
23+
- {help.commands.searchr-col}
24+
- {help.commands.searchr-cols}
25+
26+
- {help.commands.search-next}
27+
- {help.commands.searchr-next}
28+
29+
- {help.commands.search-keys}
30+
31+
## Substitution
32+
33+
- {help.commands.setcol-regex-subst}
34+
- {help.commands.setcol-regex-subst-all}
35+
36+
`Tab` to move between `search` and `replace` inputs.
37+
An empty `replace` removes the matching string.
38+
39+
# Column Creation
40+
41+
- {help.commands.addcol-regex-subst}
42+
- {help.commands.addcol-split}
43+
- {help.commands.addcol-capture}
44+
45+
## Examples
46+
47+
### Split
48+
49+
Sample input sheet **sales**:
50+
51+
date price
52+
---------- -----
53+
2024-09-01 30
54+
2024-09-02 28
55+
2024-09-03 100
56+
57+
1. [:code]:[/] (`addcol-split`) on **date** column, followed by `-` to split on hyphens.
58+
59+
date date_re price
60+
---------- ---------------- -----
61+
2024-09-01 [3] 2024; 09; 01 30
62+
2024-09-02 [3] 2024; 09; 02 28
63+
2024-09-03 [3] 2024; 09; 03 100
64+
65+
Note that the results in the `date_re` column are lists of length 3.
66+
67+
2. [:code]([/] (`expand-col`) to expand a column with lists into multiple columns with the list elements.
68+
69+
date date_re[0] date_re[1] date_re[2] price
70+
---------- ---------- ---------- ---------- -----
71+
2024-09-01 2024 09 01 30
72+
2024-09-02 2024 09 02 28
73+
2024-09-03 2024 09 03 100
74+
75+
### Substitution
76+
77+
1. On the **date** column, [:code]*[/] (`addcol-regex-subst`) and type `-`, then `Tab` to "replace" and type `,`. Then `Enter` to replace all `-` with `,`.
78+
79+
date date_re price
80+
---------- ---------- -----
81+
2024-09-01 2024,09,01 30
82+
2024-09-02 2024,09,02 28
83+
2024-09-03 2024,09,03 100
84+
85+
### Capture
86+
87+
1. On the **date** column, [:code];[/] (`addcol-capture`) and type `(\d\d\d\d)` to capture and pull out the year.
88+
89+
date date_re price
90+
---------- -------- -----
91+
2024-09-01 [1] 2024 30
92+
2024-09-02 [1] 2024 28
93+
2024-09-03 [1] 2024 100
94+
95+
Note that the results in the `date_re` column are lists of length 1.
96+
97+
2. [:code]([/] (`expand-col`) to expand a column with lists into multiple columns with the list elements.
98+
99+
date date_re[0] price
100+
---------- ---------- -----
101+
2024-09-01 2024 30
102+
2024-09-02 2024 28
103+
2024-09-03 2024 100

0 commit comments

Comments
 (0)