r""" Find the last word in a sentence. >>> last_word('abc') 'abc' >>> last_word(' abc') 'abc' >>> last_word('') '' >>> last_word(' ') '' >>> last_word('abc ') '' >>> last_word('abc def') 'def' >>> last_word('abc def ') '' >>> last_word
(
text: str,
include: Literal[
'alphanum_underscore',
'many_punctuations',
'most_punctuations',
'all_punctuations',
] = 'alphanum_underscore',
)
| 25 | |
| 26 | |
| 27 | def last_word( |
| 28 | text: str, |
| 29 | include: Literal[ |
| 30 | 'alphanum_underscore', |
| 31 | 'many_punctuations', |
| 32 | 'most_punctuations', |
| 33 | 'all_punctuations', |
| 34 | ] = 'alphanum_underscore', |
| 35 | ) -> str: |
| 36 | r""" |
| 37 | Find the last word in a sentence. |
| 38 | |
| 39 | >>> last_word('abc') |
| 40 | 'abc' |
| 41 | >>> last_word(' abc') |
| 42 | 'abc' |
| 43 | >>> last_word('') |
| 44 | '' |
| 45 | >>> last_word(' ') |
| 46 | '' |
| 47 | >>> last_word('abc ') |
| 48 | '' |
| 49 | >>> last_word('abc def') |
| 50 | 'def' |
| 51 | >>> last_word('abc def ') |
| 52 | '' |
| 53 | >>> last_word('abc def;') |
| 54 | '' |
| 55 | >>> last_word('bac $def') |
| 56 | 'def' |
| 57 | >>> last_word('bac $def', include='most_punctuations') |
| 58 | '$def' |
| 59 | >>> last_word('bac \def', include='most_punctuations') |
| 60 | '\\\\def' |
| 61 | >>> last_word('bac \def;', include='most_punctuations') |
| 62 | '\\\\def;' |
| 63 | >>> last_word('bac::def', include='most_punctuations') |
| 64 | 'def' |
| 65 | """ |
| 66 | |
| 67 | if not text: # Empty string |
| 68 | return "" |
| 69 | |
| 70 | if text[-1].isspace(): |
| 71 | return "" |
| 72 | else: |
| 73 | regex = cleanup_regex[include] |
| 74 | matches = regex.search(text) |
| 75 | if matches: |
| 76 | return matches.group(0) |
| 77 | else: |
| 78 | return "" |
| 79 | |
| 80 | |
| 81 | # This code is borrowed from sqlparse example script. |
no outgoing calls