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

Accessing matches in regular expression globbing #5427

Open
yohan-pg opened this issue May 20, 2024 · 2 comments
Open

Accessing matches in regular expression globbing #5427

yohan-pg opened this issue May 20, 2024 · 2 comments

Comments

@yohan-pg
Copy link

yohan-pg commented May 20, 2024

In regular expression globbing (or even in regular globbing), it would be nice if we could access the Match object (https://docs.python.org/3/library/re.html#match-objects) corresponding to each resulting match.

For example, replacing this:

for filepath in `data/(.*)/(.*)/.*\.png`:
    match = re.match(r'data/(.*)/(.*)/.*\.png', filepath)
    dir1, dir2 = match.groups() 

with something like:

for match in m`data/(.*)/(.*)/.*\.png`:
    dir1, dir2 = match.groups() 

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@anki-code
Copy link
Member

anki-code commented May 20, 2024

hey! Nice feature. And I believe that it's possible to implement this with backward compatibility i.e. .* and (.*) work as now but (.*)/(.*) returns pairs. It will be cool to make this shorter:

for dir1, dir2, file in `data/(.*)/(.*)/.*\.png`:
    pass

The start point to implement this:

def xonsh_pathsearch(self, pattern, pymode=False, lineno=None, col=None):

PR is welcome! (It will be cool to fix this by the way - #3372)

@yohan-pg
Copy link
Author

Sweet! I really like the idea of having it be backwards compatible. I'll try to have a look it at implementing this, although it might be in a long time.

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

No branches or pull requests

2 participants