Strutil
Simple helper functions for using strings and regular expressions (mostly) interchangeably.
Requirements
- Python 3.4+
Module Contents
strutil.is_string(obj)
Check if obj
is a string
strutil.is_regex(obj)
Check if obj
is a regular expression
strutil.replace(text, old, new, count=None, strip=False)
-
Replace an
old
subset oftext
withnew
.old
type may be either a string or regular expression.If
strip
, remove all leading/trailing whitespace.If
count
, replace the specified number of occurence, otherwise replace all.
strutil.remove(text, what, count=None, strip=False)
Likereplace
, wherenew
replacement is an empty string.
strutil.replace_each(text, items, count=None, strip=False)
Likereplace
, where each occurrence initems
is a 2-tuple of(old, new)
pair.
strutil.remove_each(text, items, count=None, strip=False)
Likeremove
, where each occurrence initems
iswhat
to remove.
strutil.contains(text, what)
Check ifwhat
occurs intext
strutil.find_first(data, what)
Search forwhat
in the iterabledata
and return the index of the first match. ReturnNone
if no match found.
strutil.splitter(text, token=None, expected=2, default='', strip=False)
Split
text
bytoken
into at leastexpected
number of results.When
token
isNone
, the default for Pythonstr.split
is used, which will split on all whitespace.
token
may also be a regex.If actual number of results is less than
expected
, pad withdefault
.If
strip
, than do just that to each result.