Skip to main content

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

Project description

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.'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

string_grab-1.3.0.tar.gz (9.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page