Stephen J. Turnbull wrote:
> I think this is a pretty unconvincing example.  While people seem to
> love to hate on regular expressions, it's hard to see how that beats
> def unquote(string: str) -> str:
>         m = re.match(r"^(?:"(.*)"|'(.*)'|(?Pvalue3))$", string)

RegEx feels overkill for this. Certainly takes longer to read, understand and 
test.

Here's a more convincing example, let's build an imaginary data format parser:

```python
data = '''\
scores:
- Matthew: 100
- David: 90

groceries:
- spam: 3
- eggs: 12
'''

parsed = {}
for line in data.splitlines():
    match line:
        case f"{heading}:"
            parsed[heading] = {}
        case f"- {name}: {count}":
            parsed[heading][name] = int(count)

print(parsed)
# Gives {'scores': {'Matthew': 100, 'David': 90}, 'groceries': {'spam': 3, 
'eggs': 12}}
```
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EOAIBI7MUW4FO5FQJVTC47VX7O4ES6J5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to