Reference¶
dsg_lib.common_functions.patterns
¶
This module contains functions for pattern searching in text using regular expressions.
The main function in this module is pattern_between_two_char
, which searches
for all patterns between two characters in a given string. The function uses
Python's built-in re
module for regex searching and the loguru
module for
logging.
Functions:
Name | Description |
---|---|
pattern_between_two_char |
str, left_characters: str, |
right_characters |
str) -> dict: Searches for all patterns between two characters (left and right) in a given string using regular expressions. |
Example
from dsg_lib.common_functions import patterns
text = "Hello, my name is 'John Doe' and I live in 'New York'." left_char =
"'" right_char = "'"
results = patterns.pattern_between_two_char(text, left_char, right_char)
print(results) ``` This will output: ```python {
'found': ['John Doe', 'New York'], 'matched_found': 2,
'pattern_parameters': {
'left_character': "'", 'right_character': "'", 'regex_pattern':
"'(.+?)'", 'text_string': "Hello, my name is 'John Doe' and I live
in 'New York'."
}
}
Author: Mike Ryan Date: 2024/05/16 License: MIT
pattern_between_two_char(text_string, left_characters, right_characters)
¶
Searches for all patterns between two characters (left and right) in a given string using regular expressions.
This function takes a string and two characters as input, and returns a dictionary containing all patterns found between the two characters in the string. The dictionary also includes the number of matches found and the regex pattern used for searching.
The function uses Python's built-in re
module for regex searching and the
loguru
module for logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text_string
|
str
|
The string in which to search for patterns. |
required |
left_characters
|
str
|
The character(s) that appear(s) immediately to |
required |
the
|
left of the desired pattern. right_characters (str
|
The |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary with the following keys: - "found": a list of strings containing all patterns found. - "matched_found": the number of patterns found. - "pattern_parameters": a dictionary with the following keys: - "left_character": the escaped left character string used to build the regex pattern. - "right_character": the escaped right character string used to build the regex pattern. - "regex_pattern": the final regex pattern used for searching. - "text_string": the escaped input string used for searching. |
Example
from dsg_lib.common_functions import patterns
text = "Hello, my name is 'John Doe' and I live in 'New York'."
left_char = "'" right_char = "'"
results = patterns.pattern_between_two_char(text, left_char, right_char)
print(results) ``` This will output: ```python {
'found': ['John Doe', 'New York'], 'matched_found': 2,
'pattern_parameters': {
'left_character': "'", 'right_character': "'", 'regex_pattern':
"'(.+?)'", 'text_string': "Hello, my name is 'John Doe' and I
live in 'New York'."
}
}
Source code in dsg_lib/common_functions/patterns.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|