kerim Posted July 24, 2017 Posted July 24, 2017 Here is a sample workflow that shows what I am trying to do. I want to do a RegEx search and replace like this: Find "apple" and replace it with a carriage return. Normally one would use \n or \r To do this in Regex, but I just end up with the character "n" or "r" in my text?
deanishe Posted July 24, 2017 Posted July 24, 2017 The right-hand box doesn't support regexes (beyond $1 etc. references to match groups). You'll probably have to use a Run Script box and do it in code. kerim 1
kerim Posted July 25, 2017 Author Posted July 25, 2017 Some research tells me that Perl is a good scripting language to do this in, and I can find examples online, but I don't know how to work them into the right format for use with Alfred Workflows. This is what I have now, but it isn't working... (trying to replace "apple" with "pear" as a test) $query = "{query}"; -p -e 's/apple/pear/g;' $query print $query;
kerim Posted July 25, 2017 Author Posted July 25, 2017 Found an example that made more sense to me. This works! $query = "{query}"; my $find = "apple"; my $replace = "pear"; $find = quotemeta $find; # escape regex metachars if present $query =~ s/$find/$replace/g; print $query;
kerim Posted July 25, 2017 Author Posted July 25, 2017 While that worked on a single line, I needed to change /g to /gm to do mult-line search. Then I needed to remove the "escape regex" line to be able to use more complex search operators. I also found that certain perl regex operators didn't seem to work for me. For instance instead of ^\s{4}- I had to enter ^ - (with actual spaces). I don't know why? I tested the above syntax on some online regex testing sites and it worked...
deanishe Posted July 25, 2017 Posted July 25, 2017 If you don't already know Perl, I'd recommend you use Ruby or Python or PHP or JavaScript. Basically, anything but Perl. Almost nobody on the forum uses Perl, so you won't get much, if any, help. What exactly are you trying to achieve?
Andrew Posted July 25, 2017 Posted July 25, 2017 If it helps, Alfred's 'Replace' workflow utility object can actually accept new lines if you alt-enter in the right field. It's likely at some point I'll take a fresh look at all of the utility objects to see how they can be improved Cheers, Andrew bonk and Toffy 1 1
Toffy Posted October 13, 2022 Posted October 13, 2022 On 7/25/2017 at 11:36 AM, Andrew said: Alfred's 'Replace' workflow utility object can actually accept new lines if you alt-enter in the right field. This is the most hidden feature I came across in a long time. It even remains invisible once you use it 😉 My experimenting did go as far as just hitting enter in the field on the right, but Alt-enter didn't occur to me. Thanks for sharing this here!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now