Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,27 @@
---
title: String to words
type: snippet
language: python
tags: [string,regexp]
cover: rocky-lake
dateModified: 2020-11-02T19:28:35+02:00
---
Converts a given string into a list of words.
- Use `re.findall()` with the supplied `pattern` to find all matching substrings.
- Omit the second argument to use the default regexp, which matches alphanumeric and hyphens.
```py
import re
def words(s, pattern = '[a-zA-Z-]+'):
return re.findall(pattern, s)
```
```py
words('I love Python!!') # ['I', 'love', 'Python']
words('python, javaScript & coffee') # ['python', 'javaScript', 'coffee']
words('build -q --out one-item', r'\b[a-zA-Z-]+\b')
# ['build', 'q', 'out', 'one-item']
```