Skip to content

Commit

Permalink
Update docs about byexample.regex as a replacement of Python's re
Browse files Browse the repository at this point in the history
  • Loading branch information
eldipa committed Nov 25, 2020
1 parent c0985aa commit 0713123
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ everything except the code between ``<pre>`` and ``</pre>`` tags.
This is what you need to write:

```python
>>> import re
>>> import byexample.regex as re
>>> from byexample.finder import ZoneDelimiter

>>> class HTMLPreBlockDelimiter(ZoneDelimiter):
Expand All @@ -64,5 +64,10 @@ or set of several extensions.
The ``zone_regex`` method should return a regular expression to find and capture
the zones.

While you can use the standard
[``re`` module](https://docs.python.org/3/library/re.html) it is
recommended to use ``byexample.regex`` which has some built-in
optimizations.

And optionally, the ``get_zone`` can be overridden to post-process the captured
string: use it to remove any spurious string that may had been captured.
9 changes: 7 additions & 2 deletions docs/contrib/how-to-support-new-finders-and-languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To accomplish this we need to create a regular expression to find the
``~~~``, where the snippet of code is and where the expected output is.

```python
>>> import re
>>> import byexample.regex as re

>>> example_re = re.compile(r'''
... # begin with ~~~
Expand Down Expand Up @@ -107,6 +107,11 @@ The ``indent`` group is to count how many spaces are not part of the example
and they are just for indentation: ``byexample`` will *drop* the first line that
has a lower level of indentation and any subsequent line.

While you can use the standard
[``re`` module](https://docs.python.org/3/library/re.html) it is
recommended to use ``byexample.regex`` which has some built-in
optimizations.

### Detect the language

Then, the finder needs to determinate in which language the example
Expand Down Expand Up @@ -351,7 +356,7 @@ you do not need to install a real ``ArnoldC`` compiler.
... output = []
... for line in source_code.split('\n'):
... if line.startswith("TALK TO THE HAND"):
... to_print = re.search(r'"([^"]*)"', line).group(1)
... to_print = re.compile(r'"([^"]*)"').search(line).group(1)
... output.append(to_print + '\n')
...
... return '\n'.join(output)
Expand Down

0 comments on commit 0713123

Please sign in to comment.