Regular Expressions

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Re: Regular Expressions Sat Jun 11, 2016 11:53 am • by YannB
Maybe it will help someone who comes across this topic… I decided to go in mode "full geek" and just installed "rename" with brew: http://brewformulas.org/rename

Then just use the "Run shell script" and in the embed script, I just went with something like that:

Code: Select all
rename 's/^(your-regex)(regex-group-2)(regex-group-3)(\..{3})$/$2$4/' *


This will rename the file with group 2 and 4 (the extension in my case, which you'll most likely want to keep most of the time ;) )
YannB
 
Posts: 6
Joined: Fri Jun 07, 2013 11:21 pm

Re: Regular Expressions Mon Jun 13, 2016 12:27 pm • by Mr_Noodle
Without more specific examples, I can't really advise but the lion's share of cases where users think they need regex, they actually can do it with the existing functionality. And many people who use regex don't use it correctly. If you can provide cases where something is only doable with regex, please provide them as it will help in guiding enhancements to the current functionality.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Regular Expressions Mon Jun 13, 2016 8:02 pm • by YannB
The regex I needed to use was:

search:

Code: Select all
^(\[.+\]\s)(.+)(\s\[.{4,5}\])(\..{3})$


replace:

Code: Select all
$2$4


I tried to use the built in functionality, but I couldn't see how to get the same thing with it.
YannB
 
Posts: 6
Joined: Fri Jun 07, 2013 11:21 pm

Re: Regular Expressions Tue Jun 14, 2016 1:26 pm • by Mr_Noodle
By examples, I don't mean the regex you came up with so much as the actual problem you are trying to solve. What kind of filenames do you need to process with this.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Regular Expressions Wed Jul 27, 2016 6:15 am • by Tekl
Hi,

I'm new to Hazel and I want to check for files where the filename begins with two letters followed by a dot and then four digits. And I have lot of files where I would like to decide by a special filename structure (receipts, bank statements, subscriptions etc,).

Tekl
Tekl
 
Posts: 3
Joined: Wed Jul 27, 2016 6:07 am

Re: Regular Expressions Wed Jul 27, 2016 7:13 am • by Tekl
Ah, sorry. I just discovered the "matches" option.
Tekl
 
Posts: 3
Joined: Wed Jul 27, 2016 6:07 am

Re: Regular Expressions Thu Aug 18, 2016 9:42 am • by dzg
Mr_Noodle wrote:By examples, I don't mean the regex you came up with so much as the actual problem you are trying to solve. What kind of filenames do you need to process with this.


I have many PDF files which are downloaded statements from banks. The names are often meaningless. I need to rename them to something meaningful, including the date of the statement.

For example, a file called 1471525044433.pdf ... from a utilitly called Endesa ... I need to run it through `pdftotext` and `grep` and `sed` to extract the statement dates:

Code: Select all
pdftotext -raw 1471525044433.pdf - | grep 'Periodo de consumo' | sed -E 's_.*: ([0-9]{2})/([0-9]{2})/([0-9]{4}) a ([0-9]{2})/([0-9]{2})/([0-9]{4})_\3-\2-\1 \6-\5-\4_g'


This gives me "2016-07-12 2016-08-09" ... and I want to rename the file to "Endesa 2016-07-12 2016-08-09.pdf"

Is this do-able in Hazel?

I would be writing different regex for each bank/source.
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Regular Expressions Thu Aug 18, 2016 10:37 am • by Mr_Noodle
How about using "Contents contain match" instead of a custom script? Looking at your regex, it looks like something Hazel can do (without having to use regex).
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Regular Expressions Thu Aug 18, 2016 10:41 am • by dzg
Mr_Noodle wrote:How about using "Contents contain match" instead of a custom script? Looking at your regex, it looks like something Hazel can do (without having to use regex).


Sorry I don't follow ... how would I grab the 'content' and then use that to rename the file?
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Regular Expressions Thu Aug 18, 2016 11:00 am • by dzg
dzg wrote:
Mr_Noodle wrote:How about using "Contents contain match" instead of a custom script? Looking at your regex, it looks like something Hazel can do (without having to use regex).


Sorry I don't follow ... how would I grab the 'content' and then use that to rename the file?


Ah, OK, I got it, something like this...
Image

BUT how would I parse BOTH dates from the content and then apply them in the rename?
Do I have to write a long custom expression that captures all 6 numbers? (day/month/year x2 for conversion to year-month-day)
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Regular Expressions Thu Aug 18, 2016 11:49 am • by dzg
dzg wrote:BUT how would I parse BOTH dates from the content and then apply them in the rename?
Do I have to write a long custom expression that captures all 6 numbers? (day/month/year x2 for conversion to year-month-day)


Aha, I figured it out ... need to give 2 different names to the match patterns... sweet !

Image
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Regular Expressions Tue Sep 20, 2016 5:40 am • by VladimirJirasek
YannB wrote:We really can't have Regex? Pretty please?

I used to send the files to Name Mangler for that, but I just updated to Hazel 4, and it doesn't run the droplet anymore… Just opens it for me to stare at it :'(

I can't imagine how it could be confusing and hinder a user in any way if you just added a "regex" option inside your rename "with pattern" thinggy.

Please reconsider! Thanks… :)


I might have a slightly more elaborate use case:

my bank creates PDF files which I can download. Unfortunately, each file has the same file name (downloadfile.pdf)!
Inside the PDF there is a statement date text, such as: Statement data 21 OCT 2016
I want to find this text in the searchable PDF and use it for the file name. So the regex would be:
Statement date ((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])-(JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC)-((1[6-9]|[2-9]\d)\d{2})

and this would be returned in first item in array

I agree that regex are complicated but in the Hazel you can make it an option to enable advanced mode so only people who really want to play with can.
VladimirJirasek
 
Posts: 3
Joined: Tue Sep 20, 2016 5:29 am

Re: Regular Expressions Tue Sep 20, 2016 10:40 am • by Mr_Noodle
Why not just use a custom date token to match that?'

Plus you understand you can use the shellscript action, right? With that, you can use regexes in the language of your choice.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Regular Expressions Tue Sep 20, 2016 11:01 am • by VladimirJirasek
VladimirJirasek wrote:
YannB wrote:We really can't have Regex? Pretty please?

I used to send the files to Name Mangler for that, but I just updated to Hazel 4, and it doesn't run the droplet anymore… Just opens it for me to stare at it :'(

I can't imagine how it could be confusing and hinder a user in any way if you just added a "regex" option inside your rename "with pattern" thinggy.

Please reconsider! Thanks… :)


I might have a slightly more elaborate use case:

my bank creates PDF files which I can download. Unfortunately, each file has the same file name (downloadfile.pdf)!
Inside the PDF there is a statement date text, such as: Statement data 21 OCT 2016
I want to find this text in the searchable PDF and use it for the file name. So the regex would be:
Statement date ((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])-(JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC)-((1[6-9]|[2-9]\d)\d{2})

and this would be returned in first item in array

I agree that regex are complicated but in the Hazel you can make it an option to enable advanced mode so only people who really want to play with can.

After posting this I found a solution using data tokens. So at least for my problem it's solved.
VladimirJirasek
 
Posts: 3
Joined: Tue Sep 20, 2016 5:29 am

Re: Regular Expressions Thu Mar 02, 2017 2:11 pm • by Sherry
dzg wrote:
dzg wrote:BUT how would I parse BOTH dates from the content and then apply them in the rename?
Do I have to write a long custom expression that captures all 6 numbers? (day/month/year x2 for conversion to year-month-day)


Aha, I figured it out ... need to give 2 different names to the match patterns... sweet !

Image


DZG, Thank you for sharing the screen shot with your solution. I'm still learning Hazel and this is exactly the way I need to create a "customize date" for Banks Statements listing Statement Periods. ex: 3/01/2017 - 3/31/2017 .
Thanks again! -Sherry-
Sherry
 
Posts: 3
Joined: Thu Jan 12, 2017 6:50 pm

PreviousNext

Return to Open Discussion

cron