Hello everybody.
This is an English translation and modification of the article (link) posted in Japanese in advance.
When I was programming in Python recently, I had a hard time finding a library for string manipulation without parentheses, so I decided to publish the code I wrote for this purpose in a library.
I'm not sure if there is enough demand for this library, so I'm holding off on releasing it on PyPI.
Release site
Project name: Splitable str
Module name: sstr
https://github.com/bigbamboo-jp/splitable-str
How to use
Example 1: Extract the words in square brackets in a sentence
from sstr import sstr
text = "Is this an [apple]? No, it's an [orange]. Then give me a [grape]."
text_ = sstr(text)
surrounded_words = []
for part in text_.divide_and_classify(enclosure=[['[', ']']]):
if part[1] == True:
word = part[0]
surrounded_words.append(word[1:-1])
print(surrounded_words) # ['apple', 'orange', 'grape']
Example 2: Count the number of "water" in a sentence (exclude the part in double quotes from the search)
from sstr import sstr
text = 'One person said that "water is infinite", but water is finite by any means.'
text_ = sstr(text)
quantity = text_.scount('water', enclosure='"')
print(quantity) # 1
You can check other usage examples in the README.
Top comments (0)