|
| 1 | +""" |
| 2 | +RegExpUtils.py |
| 3 | +This module provides utility functions for applying regular expressions. |
| 4 | +It includes methods to split strings based on whitespace, commas, periods, and punctuations. |
| 5 | +""" |
| 6 | + |
| 7 | +import re |
| 8 | + |
| 9 | + |
| 10 | +class RegExpUtils: |
| 11 | + """Class to provide utility functions for applying Regular Expressions""" |
| 12 | + |
| 13 | + @staticmethod |
| 14 | + def split_on_whitespace(text): |
| 15 | + """Splits the input text on whitespace.""" |
| 16 | + return re.split(r"(\s)", text) |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def split_on_comma_whitespace(text): |
| 20 | + """Splits the input text on commas and whitespace.""" |
| 21 | + return re.split(r"([,]|\s)", text) |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def split_on_comma_period_whitespace(text): |
| 25 | + """Splits the input text on commas, periods, and whitespace.""" |
| 26 | + return re.split(r"([,.]|\s)", text) |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def split_on_punctuations(text): |
| 30 | + """Splits the input text on various punctuations and whitespace.""" |
| 31 | + result = re.split(r'([,.:;?_!"()\']|--|\s)', text) |
| 32 | + result = [item.strip() for item in result if item.strip()] |
| 33 | + return result |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == "__main__": |
| 37 | + # Example usage |
| 38 | + print(RegExpUtils.split_on_whitespace("Hello, world. This, is a test.")) |
| 39 | + print(RegExpUtils.split_on_comma_whitespace("Hello, world. This, is a test.")) |
| 40 | + print(RegExpUtils.split_on_comma_period_whitespace("Hello, world. This, is a test.")) |
| 41 | + print(RegExpUtils.split_on_punctuations("Hello, world. Is this-- a test?")) |
0 commit comments