cornsnake.util_string
Functions for working with strings: filtering by regex, checking if is mostly empty, replacing whilst maintaining casing.
1""" 2Functions for working with strings: filtering by regex, checking if is mostly empty, replacing whilst maintaining casing. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_string.html) 5""" 6 7import re 8 9 10def filter_string_via_regex(text: str, regex: str, replacement_char: str) -> str: 11 """ 12 Filter the given string, to only characters that match the given regex. 13 - characters that do not match are replaced with 'replacement_char' 14 15 Example: filter_string_via_regex("this is a test 123 !@#", "_") -> "this_is_a_test_123____" 16 """ 17 18 def _is_ok(c: str) -> bool: 19 return True if re.match(regex, c) else False 20 21 def _process_char(c: str) -> str: 22 return c if _is_ok(c) else replacement_char 23 24 return "".join([_process_char(c) for c in text]) 25 26 27def is_empty(text: str) -> bool: 28 """ 29 Function to check if a text string is empty or '-'. 30 31 Args: 32 text (str): The text string to check. 33 34 Returns: 35 bool: True if the text is empty (None or contains only whitespace or a hyphen), False otherwise. 36 """ 37 if text is None: 38 return True 39 stripped = text.strip() 40 return stripped == "" or stripped == "-" 41 42 43def replace_keep_case(word__for_regex: str, replacement: str, text: str) -> str: 44 """ 45 Replace ocurrences of 'word__for_regex' in 'text', with 'replacement', trying to maintain the same casing. 46 47 - supports lower, titla and upper casing 48 """ 49 50 def func(match: re.Match[str]) -> str: 51 g = match.group() 52 if g.islower(): 53 return replacement.lower() 54 if g.istitle(): 55 return replacement.title() 56 if g.isupper(): 57 return replacement.upper() 58 return replacement 59 60 return re.sub(word__for_regex, func, text, flags=re.I) 61 62 63def split_into_lines(text: str, max_length: int = 200) -> list[str]: 64 """ 65 Split text into lines of maximum length at word boundaries. 66 67 Args: 68 text (str): Text to split 69 max_length (int): Maximum line length (default: 200) 70 71 Returns: 72 list: List of lines 73 """ 74 if not text: 75 return [] 76 77 result = [] 78 current_line = "" 79 80 for word in text.split(): 81 # Check if adding word would exceed max_length 82 if len(current_line) + len(word) + (1 if current_line else 0) <= max_length: 83 # Add word with a space if not the first word 84 current_line += " " + word if current_line else word 85 else: 86 # Line would be too long, start a new one 87 result.append(current_line) 88 current_line = word 89 90 # Add the last line if it has content 91 if current_line: 92 result.append(current_line) 93 94 return result
11def filter_string_via_regex(text: str, regex: str, replacement_char: str) -> str: 12 """ 13 Filter the given string, to only characters that match the given regex. 14 - characters that do not match are replaced with 'replacement_char' 15 16 Example: filter_string_via_regex("this is a test 123 !@#", "_") -> "this_is_a_test_123____" 17 """ 18 19 def _is_ok(c: str) -> bool: 20 return True if re.match(regex, c) else False 21 22 def _process_char(c: str) -> str: 23 return c if _is_ok(c) else replacement_char 24 25 return "".join([_process_char(c) for c in text])
Filter the given string, to only characters that match the given regex.
- characters that do not match are replaced with 'replacement_char'
Example: filter_string_via_regex("this is a test 123 !@#", "_") -> "this_is_a_test_123____"
28def is_empty(text: str) -> bool: 29 """ 30 Function to check if a text string is empty or '-'. 31 32 Args: 33 text (str): The text string to check. 34 35 Returns: 36 bool: True if the text is empty (None or contains only whitespace or a hyphen), False otherwise. 37 """ 38 if text is None: 39 return True 40 stripped = text.strip() 41 return stripped == "" or stripped == "-"
Function to check if a text string is empty or '-'.
Args: text (str): The text string to check.
Returns: bool: True if the text is empty (None or contains only whitespace or a hyphen), False otherwise.
44def replace_keep_case(word__for_regex: str, replacement: str, text: str) -> str: 45 """ 46 Replace ocurrences of 'word__for_regex' in 'text', with 'replacement', trying to maintain the same casing. 47 48 - supports lower, titla and upper casing 49 """ 50 51 def func(match: re.Match[str]) -> str: 52 g = match.group() 53 if g.islower(): 54 return replacement.lower() 55 if g.istitle(): 56 return replacement.title() 57 if g.isupper(): 58 return replacement.upper() 59 return replacement 60 61 return re.sub(word__for_regex, func, text, flags=re.I)
Replace ocurrences of 'word__for_regex' in 'text', with 'replacement', trying to maintain the same casing.
- supports lower, titla and upper casing
64def split_into_lines(text: str, max_length: int = 200) -> list[str]: 65 """ 66 Split text into lines of maximum length at word boundaries. 67 68 Args: 69 text (str): Text to split 70 max_length (int): Maximum line length (default: 200) 71 72 Returns: 73 list: List of lines 74 """ 75 if not text: 76 return [] 77 78 result = [] 79 current_line = "" 80 81 for word in text.split(): 82 # Check if adding word would exceed max_length 83 if len(current_line) + len(word) + (1 if current_line else 0) <= max_length: 84 # Add word with a space if not the first word 85 current_line += " " + word if current_line else word 86 else: 87 # Line would be too long, start a new one 88 result.append(current_line) 89 current_line = word 90 91 # Add the last line if it has content 92 if current_line: 93 result.append(current_line) 94 95 return result
Split text into lines of maximum length at word boundaries.
Args: text (str): Text to split max_length (int): Maximum line length (default: 200)
Returns: list: List of lines