Skip to content

Commit da273e9

Browse files
author
Andy C
committed
[doc/ref] Clarify the difference between search() and leftMatch()
- with respect to %start aka ^ - link to the YSH Regex API doc BUT I think there is a bug relative to Python -- see survey-str-api.sh. I think we should change the REG_NOTBOL logic. Unrelated: document build/doc.sh
1 parent 69d2d74 commit da273e9

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed

build/doc.sh

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
#!/usr/bin/env bash
22
#
3+
# Build docs
4+
#
35
# Usage:
46
# build/doc.sh <function name>
7+
#
8+
# Examples:
9+
#
10+
# make HTML:
11+
# $0 split-and-render doc/json.md
12+
# $0 split-and-render doc/ref/chap-type-method.md '' ../../web # need relative URL
13+
#
14+
# check code in a doc:
15+
# $0 run-code-in-doc ysh-io
16+
# $0 run-code-in-doc ref/chap-type-method
17+
#
18+
# $0 run-code-all # check all code
19+
#
20+
# build docs:
21+
# $0 all-ref
22+
# $0 all-markdown
523

6-
set -o nounset
7-
set -o pipefail
8-
set -o errexit
9-
10-
# https://oilshell.org/release/$VERSION/
11-
# doc/
12-
# index.html
13-
# INSTALL.html
14-
# INSTALL-old.html
24+
: ${LIB_OSH=stdlib/osh}
25+
source $LIB_OSH/bash-strict.sh
26+
source $LIB_OSH/task-five.sh
1527

1628
readonly OILS_VERSION=$(head -n 1 oils-version.txt)
1729
export OILS_VERSION # for quick_ref.py
@@ -999,5 +1011,5 @@ compare-golden() {
9991011
diff -r -u _release/VERSION_gold _release/VERSION/
10001012
}
10011013

1002-
"$@"
1014+
task-five "$@"
10031015

demo/survey-str-api.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,41 @@ pp test_ (''.split(/ dot /))
185185
EOF
186186
}
187187

188+
regex-match-vs-search() {
189+
echo 'REGEX PYTHON'
190+
echo
191+
192+
# This is a float
193+
python3 -c '
194+
import re
195+
196+
vowels = re.compile("[aeiou]")
197+
print(vowels.match("hi"))
198+
print(vowels.search("hi"))
199+
200+
vowelsLeft = re.compile("^[aeiou]")
201+
202+
print(vowelsLeft.match("hi", pos=1))
203+
print(vowelsLeft.search("hi", pos=1))
204+
'
205+
206+
echo
207+
echo 'REGEX YSH'
208+
echo
209+
210+
bin/ysh -c '
211+
var vowels = / [a e i o u] /
212+
echo $vowels
213+
214+
= "hi".leftMatch(vowels)
215+
= "hi".search(vowels)
216+
217+
var vowelsLeft = / %start [a e i o u] /
218+
= "hi".leftMatch(vowelsLeft, pos=1)
219+
= "hi".search(vowelsLeft, pos=1)
220+
'
221+
222+
# does JS have match vs. search? I think it might use ^
223+
}
224+
188225
"$@"

doc/ref/chap-type-method.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,38 +311,49 @@ Respects unicode.
311311

312312
### search()
313313

314-
Search for the first occurrence of a regex in the string.
314+
Search for the first occurrence of a regex in the string. Returns a
315+
[Match](#Match) value if it matches, or `null`.
315316

316317
var m = 'hi world'.search(/[a e i o u]/) # search for vowels
317-
# matches at position 1 for 'i'
318+
= m.start(0) # => index 1, matching 'i'
318319

319-
Returns a `value.Match()` if it matches, otherwise `null`.
320+
var m = 'hi world'.search(/[z]/)
321+
= m # => null
320322

321-
You can start searching in the middle of the string:
323+
---
324+
325+
The `pos` parameter lets you start searching in the middle of the string:
322326

323-
var m = 'hi world'.search(/dot 'orld'/, pos=3)
324-
# also matches at position 4 for 'o'
327+
var m = 'hi world'.search(/[a e i o u]/, pos=3)
328+
= m.start(0) # => index 4, matching 'o'
325329

326-
The `%start` or `^` metacharacter will only match when `pos` is zero.
330+
Note: the `%start` aka `^` metacharacter will only match when `pos === 0`.
327331

328332
(Similar to Python's `re.search()`.)
329333

330334
### leftMatch()
331335

332-
`leftMatch()` is like `search()`, but the pattern must match at the beginning of
333-
the string. (This is not necessarily the same as including `%start` in the pattern.)
336+
`leftMatch()` is like `search()`, but the pattern must match at the beginning
337+
of the string.
338+
339+
var m = 'hi world'.leftMatch(/[a e i o u]/)
340+
= m # => Null because 'h' is not a vowel
334341

335-
# match if the first char is a vowel
336-
var m = 'hi world'.leftMatch(/[a e i o u]/) # => Null, 'h' is not a vowel
342+
var m = 'ale'.leftMatch(/[a e i o u]/)
343+
= m.start(0) # => index 0 for a
337344

338-
var m = 'aye'.leftMatch(/[a e i o u]/)
339-
# matches 'a'
345+
(Unlike `search()`, the `%start` aka `^` metcharacter may match when `pos !==
346+
0`.)
347+
348+
---
340349

341350
`leftMatch()` Can be used to implement lexers that consume every byte of input.
342351

343352
var lexer = / <capture digit+> | <capture space+> /
344353

345-
(Similar to Python's `re.match()`.)
354+
See [YSH Regex API](../ysh-regex-api.html).
355+
356+
(`leftMatch()` is similar to Python's `re.match()`.)
346357

347358
### split()
348359

0 commit comments

Comments
 (0)