To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 633178d4 authored by vermeul's avatar vermeul
Browse files

added regex module

parent f1b5d670
......@@ -170,4 +170,48 @@ alias = alias[0] if alias else attribute
```
</strong>
In this case, our alias will be the same as the attribute, if it was not explicitly defined.
In this case, our alias will be the same as the attribute, if it was not explicitly defined.
## repeated captures: use the `regex` module.
The usual `re` does not support repeated captures, it always only returns the last capture. Use the `regex` module instead.
In the example below, we would like to extract all keys and values after `literals:
```python
import regex
import re
conf = """configMapGenerator:
- name: api-env
behavior: merge
literals:
- ADMIN_EMAIL=chucknorris@roundhouse.gov
- SERVICE_HOSTNAME=...
- ALLOWED_HOSTS=localhost,...
- LOGSTASH_HOST=...
- DEPLOYMENT_NAME=staging
- name: frontend-env
behavior: merge
literals:
- NEXT_PUBLIC_IMAGES_DOMAINS=localhost
"""
reg = regex.compile(
r"""\s+literals\:\n
(?:
\s+-\s+
(?P<key>.*?)
\s*=\s*
(?P<value>.*?)
\n
)+
""", re.X
)
for m in reg.finditer(conf):
print(m.capturesdict())
{'key': ['ADMIN_EMAIL', 'SERVICE_HOSTNAME', 'ALLOWED_HOSTS', 'LOGSTASH_HOST', 'DEPLOYMENT_NAME'], 'value': ['chucknorris@roundhouse.gov', '...', 'localhost,...', '...', 'staging']}
{'key': ['NEXT_PUBLIC_IMAGES_DOMAINS'], 'value': ['localhost']}
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment