Spaces:
Sleeping
Sleeping
import re | |
def is_newline_after_text(text1, text2): | |
""" | |
Checks if text1 is in text2 and if the next non-space character after text1 is a newline. | |
Args: | |
text1: The text to search for. | |
text2: The text to search within. | |
Returns: | |
A tuple: (True/False if text1 is found, True/False if next char is newline, or None if not found) | |
""" | |
match = re.search(re.escape(text1), text2) #escape text1 to handle special characters | |
if match: | |
# Find the next non-space character | |
next_char_index = match.end() | |
while next_char_index < len(text2) and text2[next_char_index].isspace(): | |
next_char_index += 1 | |
if text2[next_char_index:next_char_index+2] == r'\n': | |
print("newline found") | |
if next_char_index < len(text2) and text2[next_char_index:next_char_index+2] == r'\n': | |
return True | |
return False | |
def is_newline_after_text_2(text1, text2): | |
""" | |
Checks if text1 is in text2 and if the next non-space character after text1 is a newline. | |
Args: | |
text1: The text to search for. | |
text2: The text to search within. | |
Returns: | |
True if next char is newline | |
""" | |
text2 = text2.replace("\n", "\\n") | |
ater_text = text2.split(text1) | |
if len(ater_text) > 1: | |
ater_text = ater_text[1].lstrip() # Remove spaces | |
if ater_text.startswith('\n'): | |
return True | |
return False | |
# Example usage: | |
text1 = "hello" | |
text2 = "some text hello \nmore text" | |
result = is_newline_after_text_2(text1, text2) | |
print(f"Next char is newline: {result}\n") | |
text1 = "hello" | |
text2 = "some text hello more text" | |
result = is_newline_after_text_2(text1, text2) | |
print(f"Next char is newline: {result}\n") | |
text1 = "hello" | |
text2 = "some text hello \nmore text" | |
result = is_newline_after_text_2(text1, text2) | |
print(f"Next char is newline: {result}\n") | |
text1 = "hello" | |
text2 = "some text hello\t\nmore text" #test tab space before newline | |
result = is_newline_after_text_2(text1, text2) | |
print(f"Next char is newline: {result}\n") | |
text1 = "hello." #test special characters | |
text2 = "some text hello. \nmore text" | |
result = is_newline_after_text_2(text1, text2) | |
print(f"Next char is newline: {result}\n") |