Skip to content

Commit 4c17c55

Browse files
committed
Added robust string replacement.
Closing #34.
1 parent d318361 commit 4c17c55

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

docs/src/lecture_06/lab.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,12 @@ The naive solution
364364
sreplace_i(s) = replace(s, 'i' => 'k')
365365
@test Meta.parse(sreplace_i(s)) == replace_i(Meta.parse(s))
366366
```
367-
does not work in this simple case, because it will replace "i" inside the `sin(z)` expression. Avoiding these corner cases would require some more involved logic (like complicated regular expressions in `replace`), therefore using the parsed AST is preferable when manipulating the code.
368-
367+
does not work in this simple case, because it will replace "i" inside the `sin(z)` expression. We can play with regular expressions to obtain something, that is more robust
368+
```@repl lab06_meta
369+
sreplace_i(s) = replace(s, r"([^\w]|\b)i(?=[^\w]|\z)" => s"\1k")
370+
@test Meta.parse(sreplace_i(s)) == replace_i(Meta.parse(s))
371+
```
372+
however the code may now be harder to read. Thus it is preferable to use the parsed AST when manipulating Julia's code.
369373
```@raw html
370374
</p></details>
371375
```

0 commit comments

Comments
 (0)