diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 923d2230..9e923a4b 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -1,3 +1,4 @@ from swarms.utils.display_markdown import display_markdown_message from swarms.utils.futures import execute_futures_dict from swarms.utils.code_interpreter import SubprocessCodeInterpreter +from swarms.utils.parse_code import extract_code_in_backticks_in_string \ No newline at end of file diff --git a/swarms/utils/parse_code.py b/swarms/utils/parse_code.py index e69de29b..1ce1d344 100644 --- a/swarms/utils/parse_code.py +++ b/swarms/utils/parse_code.py @@ -0,0 +1,11 @@ +import re + + +def extract_code_in_backticks_in_string(message: str) -> str: + """ + To extract code from a string in markdown and return a string + + """ + pattern = r"`` ``(.*?)`` " # Non-greedy match between six backticks + match = re.search(pattern, message, re.DOTALL) # re.DOTALL to match newline chars + return match.group(1).strip() if match else None \ No newline at end of file