Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No empty {} placeholders #138

Open
dejudicibus opened this issue Nov 16, 2021 · 1 comment
Open

No empty {} placeholders #138

dejudicibus opened this issue Nov 16, 2021 · 1 comment

Comments

@dejudicibus
Copy link

Let us suppose that I write

pattern = ">{}<"
html = "Mr. <strong>George</strong> is a <em>beautiful</em> cat"
print(''.join(part[0] for part in findall(pattern, html)))

I get

Mr. George is a beautiful

To fix that I can write

pattern = ">{}<"
html = "<p>Mr. <strong>George</strong> is a <em>beautiful</em> cat</p>"
print(''.join(part[0] for part in findall(pattern, html)))

and now I get

Mr. George is a beautiful cat

However, if I write

pattern = ">{}<"
html = "<p><strong>George</strong> is a <em>beautiful</em> cat</p>"
print(''.join(part[0] for part in findall(pattern, html)))

I get

<strong>George is a beautiful cat

It looks like the {} placeholder does not like to be empty.

@sdementen
Copy link

Would this achieve what you want ?

pattern = ">{:opt}<"
html = "<p><strong>George</strong> is a <em>beautiful</em> cat</p>"
print(''.join(part[0] for part in findall(pattern, html,extra_types=dict(opt=with_pattern(r'.*?')(lambda x:x)))))

outputs
George is a beautiful cat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants