string-grab

Extract one or multiple substrings between two start and end strings.


Keywords
pypi, pypi-package, string-manipulation
License
GPL-2.0
Install
pip install string-grab==1.3.0

Documentation

string_grab

Extract one or multiple substrings between two start and end strings.

Functions: grab, grab_all, grab_until, grab_after, inject, inject_until, inject_after.

from string_grab import grab

text = '''Gender: female
       Race: White
       Birthday: 3/23/1973
       Mobile: 715-523-1076
       Mobile: 715-563-3967
       Street: 4674 Lynn Avenue
       City, State, Zip: Eau Claire, Wisconsin(WI), 54701'''

birthday = grab(text, start='Birthday: ', end='\n')
print(birthday)

>> '3/23/1973'


Main functions

grab

Returns the first substring between two 'start' and 'end' strings.

Variants: grab_all, grab_until, grab_after.

grab('John likes apples.',
     start='John likes ',
     end='.')

>> 'apples'

inject

Inserts a substring, replacing everything between two 'start' and 'end' strings.

Variants: inject_until, inject_after.

inject('John likes apples.',
       'oranges',
       start='John likes ',
       end='.')

>> 'John likes oranges.'


grab variants

grab_all

Yields all substrings between two 'start' and 'end' strings.

See grab.

results = grab_all('John likes apples. John likes oranges.',
                   start='likes ',
                   end='.')

list(results)
>> ['apples', 'oranges']

grab_until

Returns everything before the 'end' string.

See grab.

grab_until('John likes apples.',
           end=' likes')

>> 'John'

grab_after

Returns everything after the 'start' string.

See grab.

grab_after('John likes apples.',
           start='likes ')

>> 'apples.'


inject variants

inject_until

Inserts a substring, replacing everything before the 'end' string.

See inject.

inject_until('John likes apples.',
             'Sarah',
             end=' likes')

>> 'Sarah likes apples.'

inject_after

Inserts a substring, replacing everything after the 'start' string.

See inject.

inject_after('Sarah likes apples.',
             'oranges.',
             start='likes ')

>> 'Sarah likes oranges.'