Skip to content

Instantly share code, notes, and snippets.

@shner-elmo
Created October 5, 2023 22:34
Show Gist options
  • Save shner-elmo/7cd7c383fa882ab8cda743ec7a689b24 to your computer and use it in GitHub Desktop.
Save shner-elmo/7cd7c383fa882ab8cda743ec7a689b24 to your computer and use it in GitHub Desktop.
Regex split test cases (feel free to add more)
import re
lst = [
(
r"(x)",
"1x2",
["1", "x", "2"],
),
(
r"([^\d]+)",
"1-2",
["1", "-", "2"],
),
(
r"(\s+)",
"this is a \n\ntest",
["this", " ", "is", " ", "a", " \n\n", "test"],
),
(
r"([^0-9a-zA-Z_])",
" C# is great! (not actually) :)",
["", " ", "C", "#", "", " ", "is", " ", "great", "!", "", " ", "", "(", "not", " ", "actually", ")", "", " ", "", ":", "", ")", ""],
),
(
r"([a-zA-Z](?:[a-zA-Z']*[a-zA-Z])?)",
'He said, "I\'d like to eat cake!"',
["", "He", " ", "said", ', "', "I'd", " ", "like", " ", "to", " ", "eat", " ", "cake", '!"'],
),
]
def main():
for pattern, in_text, expected_out in lst:
out = re.split(pattern, in_text)
assert out == expected_out, (f'Regex: {pattern}, Text: {in_text}, \nOut: {out} '
f'\nExpected out: {expected_out}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment