From fc484d2d37c1a988190721d4ef3816f652c28d26 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 25 Oct 2023 00:01:30 -0400 Subject: [PATCH] parse code function in utils Former-commit-id: e4c2de3c549c9136fb7eb7abada951e747a62e44 --- swarms/utils/__init__.py | 1 + swarms/utils/parse_code.py | 11 +++++++++++ 2 files changed, 12 insertions(+) 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