diff --git a/CHANGELOG.md b/CHANGELOG.md index f9d161e..ae2b523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. The format ## [Unreleased] ### Fixed +- Skip unmatched optional capture groups in `Page.search()` when `main_group` selects them. - Initialize PDFium's form environment in `get_page_image` so that filled AcroForm field content is included when rendering pages via `Page.to_image()`. ([#1367](https://github.com/jsvine/pdfplumber/issues/1367)) - Fix `make venv`, which created the virtual environment at `venv/` but then installed into `${VENV}` (default `.venv/`), causing the target to fail on a fresh checkout (h/t @soodoku). ([ec96f72](https://github.com/jsvine/pdfplumber/commit/ec96f72)) - Include the wrapped exception's class name in `PdfminerException`'s message when `pdfminer.six` raises an exception without one (e.g. `PDFPasswordIncorrect`), which previously surfaced as a blank error message. diff --git a/README.md b/README.md index dc23ac9..67db2bf 100644 --- a/README.md +++ b/README.md @@ -577,6 +577,7 @@ Many thanks to the following users who've contributed ideas, features, and fixes - [Sebastian Cao](https://github.com/cycsmail) - [Kaspar Naraghi](https://github.com/kaninaba94) - [Siddharth Gaur](https://github.com/siddharthgaur1) +- [Jake Wang](https://github.com/jakezwang) ## Contributing diff --git a/pdfplumber/utils/text.py b/pdfplumber/utils/text.py index 1601f9c..69e8273 100644 --- a/pdfplumber/utils/text.py +++ b/pdfplumber/utils/text.py @@ -198,7 +198,7 @@ def search( gen = re.finditer(compiled, self.as_string) # Remove zero-length matches (can happen, e.g., with optional # patterns in regexes) and whitespace-only matches - filtered = filter(lambda m: bool(m.group(main_group).strip()), gen) + filtered = filter(lambda m: bool((m.group(main_group) or "").strip()), gen) return [ self.match_to_dict( m, diff --git a/tests/test_utils.py b/tests/test_utils.py index 18e5ce8..e3cf875 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -470,6 +470,22 @@ def test_search_string(self): results = page.search(r"10 Tuesday", layout=True) assert len(results) == 0 + def test_search_optional_main_group(self): + page = self.pdf_scotus.pages[0] + assert page.search(r"(sdfsd)?", main_group=1) == [] + + def test_search_alternative_main_group(self): + page = self.pdf_scotus.pages[0] + results = page.search( + r"Tuesday|(supreme\s+court)", + main_group=1, + case=False, + return_groups=False, + ) + expected = page.search(r"supreme\s+court", case=False, return_groups=False) + assert len(expected) == 2 + assert results == expected + def test_extract_text_lines(self): page = self.pdf_scotus.pages[0] results = page.extract_text_lines()