String utils

re_find(regex, s, flags=0)

Finds regex in s, returning the match in the simplest possible form guessed by captures in given regular expression:

Captures Return value
no captures a matched string
single positional capture a substring matched by capture
only positional captures a tuple of substrings for captures
only named captures a dict of substrings for captures
mixed pos/named captures a match object

Returns None on mismatch.

# Find first number in a line
silent(int)(re_find(r'\d+', line))

# Find number of men in a line
re_find(r'(\d+) m[ae]n', line)

# Parse uri into nice dict
re_find(r'^/post/(?P<id>\d+)/(?P<action>\w+)$', uri)
re_test(regex, s, flags=0)

Tests whether regex can be found in s.

re_all(regex, s, flags=0)
re_iter(regex, s, flags=0)

Returns a list or iterator of all matches of regex in s. Matches are presented in most simple form possible, see table in re_find() docs.

# A fast and dirty way to parse ini section into dict
dict(re_iter('(\w+)=(\w+)', ini_text))
re_finder(regex, flags=0)

Returns a function that calls re_find() for it’s sole argument. It’s main purpose is quickly constructing mapper functions for map() and friends.

See also Extended function semantics.

re_tester(regex, flags=0)

Returns a function that calls re_test() for it’s sole argument. Aimed at quick construction of predicates for use in filter() and friends.

See also Extended function semantics.

str_join([sep="", ]seq)

Joins sequence with sep. Same as sep.join(seq), but forcefully converts all elements to separator type, str by default.

See also joining().

cut_prefix(s, prefix)

Cuts prefix from given string if it’s present.

cut_suffix(s, suffix)

Cuts suffix from given string if it’s present.