Skip to content

Commit

Permalink
docs: improve re_replace docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCrews authored and cpcloud committed Oct 13, 2023
1 parent 1d264dc commit f55d0db
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions ibis/expr/types/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ def re_replace(
pattern: str | StringValue,
replacement: str | StringValue,
) -> StringValue:
r"""Replace match found by regex `pattern` with `replacement`.
r"""Replace all matches found by regex `pattern` with `replacement`.
Parameters
----------
Expand All @@ -1104,16 +1104,51 @@ def re_replace(
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "bac", "bca"]})
>>> t.s.re_replace("^(a)", "b")
>>> t = ibis.memtable(
... {"s": ["abc", "bac", "bca", "this has multi \t whitespace"]}
... )
>>> s = t.s
Replace all "a"s that are at the beginning of the string with "b":
>>> s.re_replace("^a", "b")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ RegexReplace(s, '^a', 'b') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├───────────────────────────────┤
│ bbc │
│ bac │
│ bca │
│ this has multi \t whitespace │
└───────────────────────────────┘
Double up any "a"s or "b"s, using capture groups and backreferences:
>>> s.re_replace("([ab])", r"\0\0")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ RegexReplace(s, '()', '\\0\\0') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├─────────────────────────────────────┤
│ aabbc │
│ bbaac │
│ bbcaa │
│ this haas multi \t whitespaace │
└─────────────────────────────────────┘
Normalize all whitespace to a single space:
>>> s.re_replace("\s+", " ")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ RegexReplace(s, '^(a)', 'b') ┃
┃ RegexReplace(s, '\\s+', ' ') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├──────────────────────────────┤
bbc
abc
│ bac │
│ bca │
│ this has multi whitespace │
└──────────────────────────────┘
"""
return ops.RegexReplace(self, pattern, replacement).to_expr()
Expand Down

0 comments on commit f55d0db

Please sign in to comment.