Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(block): Support find all regex extraction for ExtractTextInformationBlock (#8934)

Zamil Majdyd827d4f9e4e3
diff --git a/autogpt_platform/backend/backend/blocks/text.py b/autogpt_platform/backend/backend/blocks/text.py
index b7b6c73fff..d1e59cb831 100644
--- a/autogpt_platform/backend/backend/blocks/text.py
+++ b/autogpt_platform/backend/backend/blocks/text.py
@@ -71,6 +71,7 @@ class ExtractTextInformationBlock(Block):
description="Case sensitive match", default=True
)
dot_all: bool = SchemaField(description="Dot matches all", default=True)
+ find_all: bool = SchemaField(description="Find all matches", default=False)
class Output(BlockSchema):
positive: str = SchemaField(description="Extracted text")
@@ -88,12 +89,27 @@ class ExtractTextInformationBlock(Block):
{"text": "Hello, World!", "pattern": "Hello, (.+)", "group": 0},
{"text": "Hello, World!", "pattern": "Hello, (.+)", "group": 2},
{"text": "Hello, World!", "pattern": "hello,", "case_sensitive": False},
+ {
+ "text": "Hello, World!! Hello, Earth!!",
+ "pattern": "Hello, (\\S+)",
+ "group": 1,
+ "find_all": False,
+ },
+ {
+ "text": "Hello, World!! Hello, Earth!!",
+ "pattern": "Hello, (\\S+)",
+ "group": 1,
+ "find_all": True,
+ },
],
test_output=[
("positive", "World!"),
("positive", "Hello, World!"),
("negative", "Hello, World!"),
("positive", "Hello,"),
+ ("positive", "World!!"),
+ ("positive", "World!!"),
+ ("positive", "Earth!!"),
],
)
@@ -105,15 +121,21 @@ class ExtractTextInformationBlock(Block):
flags = flags | re.DOTALL
if isinstance(input_data.text, str):
- text = input_data.text
- else:
- text = json.dumps(input_data.text)
-
- match = re.search(input_data.pattern, text, flags)
- if match and input_data.group <= len(match.groups()):
- yield "positive", match.group(input_data.group)
+ txt = input_data.text
else:
- yield "negative", text
+ txt = json.dumps(input_data.text)
+
+ matches = [
+ match.group(input_data.group)
+ for match in re.finditer(input_data.pattern, txt, flags)
+ if input_data.group <= len(match.groups())
+ ]
+ for match in matches:
+ yield "positive", match
+ if not input_data.find_all:
+ return
+ if not matches:
+ yield "negative", input_data.text
class FillTextTemplateBlock(Block):