File: features.md

package info (click to toggle)
txt2regex 0.9-5
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 572 kB
  • sloc: sh: 1,598; makefile: 108
file content (233 lines) | stat: -rw-r--r-- 6,870 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Feature tests for txt2regex

This is file is both documentation and a test file, showing how some txt2regex features work in practice, with the command line options required to trigger them and their expected result.

The [clitest](https://github.com/aureliojargas/clitest) tool can identify and run all the commands listed here and check if their actual output matches the expected one. Just run `clitest tests/features.md`.

## Setup

Make sure all the commands use the same Bash version and the same txt2regex file.

```console
$ txt2regex() { bash ./txt2regex.sh "$@"; }
$
```

## User input: Numbers — getNumber()

When informing numbers and non-numbers (`a5!6` in this test) when prompted for a number, the non-numbers are silently removed.

```console
$ txt2regex --prog egrep --history '215¤a5!6'
 Regex egrep: .{56}

$
```

## User input: Remove duplicated chars from [] — getCharList()

When informing literal characters to be put inside a `[]` list, txt2regex will deduplicate those characters, because the repetition in this case is not meaningful (`[aabbcc]` is the same as `[abc]`).

```console
$ txt2regex --prog egrep --history '24¤aabbbcab'
 Regex egrep: [abc]

$
```

## User input: Rearrange [] special elements — getCharList()

When informing literal characters to be put inside a `[]` list, some special cases have to be handled:

- `^` must not be the first char, otherwise it would mean a negated list
- `-` must not be between two other chars, otherwise it would mean a range.
- `]` must be the very first char, otherwise it would end the list prematurely.
- `[` is not special since the list is already opened, nothing to be done in this case.

```console
$ txt2regex --prog egrep --history '24¤^abc'  # move ^ to the last position
 Regex egrep: [abc^]

$ txt2regex --prog egrep --history '24¤a^bc'  # ^ is not special in the 2nd position
 Regex egrep: [a^bc]

$ txt2regex --prog egrep --history '24¤a-bc'  # move - to the last position
 Regex egrep: [abc-]

$ txt2regex --prog egrep --history '24¤-abc'  # - is not special in the 1st position
 Regex egrep: [-abc]

$ txt2regex --prog egrep --history '24¤a]bc'  # move ] to the 1st position
 Regex egrep: []abc]

$ txt2regex --prog egrep --history '24¤a[bc'  # [ is not special
 Regex egrep: [a[bc]

$ txt2regex --prog egrep --history '24¤^a[b-c]'  # everything together
 Regex egrep: []a[bc^-]

$
```

## User input: Escape \ when inside [] — escCharList()

In some programs, it's required to escape the `\` character when using it inside `[]` lists, making it `\\` or even `\\\\`.

```console
$ txt2regex --all --history '241¤\'
 Regex awk       : [\\]
 Regex chicken   : [\\\\]
 Regex ed        : [\]
 Regex egrep     : [\]
 Regex emacs     : [\\\\]
 Regex expect    : [\\]
 Regex find      : [\]
 Regex gawk      : [\\]
 Regex grep      : [\]
 Regex javascript: [\\]
 Regex lex       : [\\]
 Regex mawk      : [\\]
 Regex mysql     : [\\\\]
 Regex perl      : [\\]
 Regex php       : [\\\\]
 Regex postgres  : [\\]
 Regex procmail  : [\]
 Regex python    : [\\]
 Regex sed       : [\]
 Regex tcl       : [\\]
 Regex vi        : [\]
 Regex vim       : [\\]

$
```

## User input: Escape special chars — escChar()

The user has typed `.*+?[]{}()|^$\` as a literal string.

Every metacharacter should be escaped so it will match as a literal character.

```console
$ txt2regex --all --history '23¤.*+?[]{}()|^$\'
 Regex awk       : \.\*\+\?\[]{}\(\)\|\^\$\\
 Regex chicken   : \\.\\*\\+\\?\\[]{}\\(\\)\\|\\^\\$\\\\
 Regex ed        : \.\*+?\[]{}()|^$\\
 Regex egrep     : \.\*\+\?\[]\{}\()\|\^\$\\
 Regex emacs     : \\.\\*\\+\\?\\[]{}()|^$\\\\
 Regex expect    : \.\*\+\?\[]\{\}\(\)\|\^\$\\
 Regex find      : \.\*\+\?\[]\{}\()\|\^\$\\
 Regex gawk      : \.\*\+\?\[]{}\()\|\^\$\\
 Regex grep      : \.\*+?\[]{}()|^$\\
 Regex javascript: \.\*\+\?\[]{}\(\)\|\^\$\\
 Regex lex       : \.\*\+\?\[]\{\}\(\)\|^$\\
 Regex mawk      : \.\*\+\?\[]{}\(\)\|\^\$\\
 Regex mysql     : \\.\\*\\+\\?\\[]{}\\()\\|\\^\\$\\\\
 Regex perl      : \.\*\+\?\[]\{}\(\)\|\^\$\\
 Regex php       : \\.\\*\\+\\?\\[]\\{}\\(\\)\\|\\^\\$\\\\
 Regex postgres  : \.\*\+\?\[]{}\(\)\|\^\$\\
 Regex procmail  : \.\*\+\?\[]{}\(\)\|\^\$\\
 Regex python    : \.\*\+\?\[]\{}\(\)\|\^\$\\
 Regex sed       : \.\*+?\[]{}()|^$\\
 Regex tcl       : \.\*\+\?\[]\{\}\(\)\|\^\$\\
 Regex vi        : \.\*+?\[]{}()|^$\\
 Regex vim       : \.\*+?\[]{}()|^$\\

$
```

Now try some Bash special chars to make sure nothing will break. Those chars should not be escaped since they are not metacharacters.

```console
$ txt2regex --prog egrep --history '23¤#!&;/`"%>'
 Regex egrep: #!&;/`"%>

$
```

## User input: Use all "special combination" options — getCombo()

Turn on all the options under the "a special combination" menu.

```console
$ txt2regex --prog sed --history '26¤:012345'
 Regex sed: [A-Za-z0-9_ \t]

$
```

## User input: Use all POSIX options — getPosix()

Turn on all the options under the "a POSIX combination (locale aware)" menu.

```console
$ txt2regex --prog egrep --history '27¤:01234567'
 Regex egrep: [[:alpha:][:lower:][:upper:][:digit:][:alnum:][:xdigit:][:blank:][:graph:]]

$
```

## POSIX support — getHasPosix()

If the program does not support POSIX character classes, a `!!` string is shown instead.

```console
$ txt2regex --all --history '27¤:0'
 Regex awk       : [[:alpha:]]
 Regex chicken   : [[:alpha:]]
 Regex ed        : [[:alpha:]]
 Regex egrep     : [[:alpha:]]
 Regex emacs     : [[:alpha:]]
 Regex expect    : [[:alpha:]]
 Regex find      : [[:alpha:]]
 Regex gawk      : [[:alpha:]]
 Regex grep      : [[:alpha:]]
 Regex javascript: !!
 Regex lex       : [[:alpha:]]
 Regex mawk      : !!
 Regex mysql     : [[:alpha:]]
 Regex perl      : [[:alpha:]]
 Regex php       : [[:alpha:]]
 Regex postgres  : [[:alpha:]]
 Regex procmail  : !!
 Regex python    : !!
 Regex sed       : [[:alpha:]]
 Regex tcl       : [[:alpha:]]
 Regex vi        : [[:alpha:]]
 Regex vim       : [[:alpha:]]

$
```

## Tab inside [] — getListTab()

If the program does not support using `\t` inside `[]` lists to represent a tab character, a `<TAB>` string is shown instead.

This is a reminder for the user that this string must be replaced by a literal tab to be able to use the regex.

```console
$ txt2regex --all --history '26¤:5'
 Regex awk       : [\t]
 Regex chicken   : [\t]
 Regex ed        : [<TAB>]
 Regex egrep     : [<TAB>]
 Regex emacs     : [\t]
 Regex expect    : [\t]
 Regex find      : [<TAB>]
 Regex gawk      : [\t]
 Regex grep      : [<TAB>]
 Regex javascript: [\t]
 Regex lex       : [\t]
 Regex mawk      : [\t]
 Regex mysql     : [\t]
 Regex perl      : [\t]
 Regex php       : [\t]
 Regex postgres  : [\t]
 Regex procmail  : [<TAB>]
 Regex python    : [\t]
 Regex sed       : [\t]
 Regex tcl       : [\t]
 Regex vi        : [<TAB>]
 Regex vim       : [\t]

$
```