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

Add re.findall to pick out re matches #805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jamstah
Copy link
Contributor

@Jamstah Jamstah commented Mar 9, 2024

Actually using re.finditer so we can apply a repl to the result. This allows users to pick out matches and reformat them in one step.

Fixes #804

@Jamstah
Copy link
Contributor Author

Jamstah commented Mar 9, 2024

Draft because it needs documentation.

@Jamstah Jamstah marked this pull request as ready for review March 10, 2024 17:59
docs/source/filters.rst Outdated Show resolved Hide resolved
Copy link
Owner

@thp thp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I don't mind a re.findall(), but people might be tempted to use it to "parse" HTML (which is not possible using regex) when the CSS or XPath filters are more suited for the job.

If we make it clear in the docs when it isn't and when it is the right tool for the job, I'm happy to merge this :)

@Jamstah
Copy link
Contributor Author

Jamstah commented Mar 12, 2024

Yeah, I was thinking that but was too lazy to think of a good example :)

I've borrowed something from another test, and added some doc.

lib/urlwatch/filters.py Outdated Show resolved Hide resolved
lib/urlwatch/filters.py Outdated Show resolved Hide resolved
Copy link
Owner

@thp thp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments.

Actually using re.finditer so we can apply a repl to the result. This
allows users to pick out matches and reformat them in one step.

Fixes thp#804

Signed-off-by: James Hewitt <james.hewitt@uk.ibm.com>
Copy link
Owner

@thp thp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments, rest looks good. Thanks!

following example applies the filter 3 times:
You can use ``re.sub`` and ``re.findall`` to apply regular expressions.

``re.sub`` can be used to remove of replace all non-overlapping instances
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``re.sub`` can be used to remove of replace all non-overlapping instances
``re.sub`` can be used to remove or replace all non-overlapping instances

url: https://example.com/regex-findall.html
filter:
- re.findall:
pattern: 'The next draw is on (\d\d\d\d-\d\d-\d\d).'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
pattern: 'The next draw is on (\d\d\d\d-\d\d-\d\d).'
pattern: 'The next draw is on (\d{4}-\d{2}-\d{2}).'

- re.findall:
pattern: 'The next draw is on (\d\d\d\d-\d\d-\d\d).'
repl: '\1'
- re.findall: '[0-9]+'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
- re.findall: '[0-9]+'
- re.findall: '\d+'

Comment on lines +538 to +539
XPATH expressions. HTML and XML cannot be parsed properly using regular
expressions. If the CSS selector or XPATH cannot provide the targeted
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a link to https://stackoverflow.com/a/1732454/1047040 on "cannot be parsed" for extra entertainment.

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

Successfully merging this pull request may close these issues.

Feature request: Extension of regex filtering to extract data
2 participants