Sorry, more nubie questions - this seems to work on a single instance, but does not seem to replace all instances. Ideas?
/find this string/g
If you have special characters in the string that regex uses, you might hit problems because they would need to be escaped. Like “.” you would have to write as “.”
I started with regex and basic find and replace sorts of things. The satisfaction you get from making yourself a truly custom and useful tool is amazing. And Drafts is, I think, an ideal playground to learn this stuff.
Hi there - I’m finally back to this, and am hitting some dead ends. I wondered if anyone can help me here? I’m trying to translate annotations brought into drafts from PDF expert into text that can then be exported to bookends and then as into single text files in Devonthink (the “Split at Delimiter” action is a life saver!).
But I’m having a tough time getting my regex to work. I want to take chunks of text that look like this:
“highlight [page 17]: As a result, the idea that techniques are uncomplicatedly grasped, related to the efficient exploitation of materials can be questioned, a point also made by various anthropologies of technology over the previous two decades (e.g. Lemonnier 1993).
highlight [page 19]: Here, note how only scientific analysis can determine whether distant cherts were objectively…”
and have them transformed to this:
As a result, the idea that techniques are uncomplicatedly grasped, related to the efficient exploitation of materials can be questioned, a point also made by various anthropologies of technology over the previous two decades (e.g. Lemonnier 1993).
Here, note how only scientific analysis can determine whether distant cherts were objectively…
So I am trying to do two find and replaces. The first takes “Highlight [page” and replaces it with “#@”. The second to take “]:” and replaces it with a carriage return. I can do this without any problem in the “find and replace” process, but having an action would speed things up. I seem to be having a real battle with the square brackets here: “[” doesn’t seem to get the action to work.
Usually you escape regex by using a slash:
I have a similar action that might be useful to look at: link. But I just strip out the whole line so I don’t need to deal with the brackets.
Hi - thanks (but link doesn’t seem to be working). I am really rough with Regex, so I suspect I’m doing something silly here…
https://regex101.com/ is a good site to test out RegEx when working on scripts, but it looks to me like there might be a space between the slash and the bracket in your script?
Nope, no space there. Hmmm. Can you share the link your action again? Perhaps it can give me some leads…
Here you go: https://actions.getdrafts.com/a/1Fu
I think this might be the sort of thing you are after.
// define regex to use... //Drop the double quotes from the original and add a space after page const findRegex = /\[page /g; // define replacement expression... const replaceWith = "#@"; // do the replacement ... draft.content = draft.content.replace(findRegex, replaceWith); //Do some more replacements... draft.content = draft.content.replace(/^highlight #@/gm, "#@"); draft.content = draft.content.replace(/(#@[0-9]+)]: /g, "$1\n"); //Update the draft draft.update;
Hope that helps.
This is great - thank you both for your help with this!
Great - thanks for sharing!
Just as an addendum, please also write Readdle and ask them to offer better export options for annotations, including turning off or modifying the “highlight” text, and offering direct export to Drafts rather than having to copy and paste from an email draft… I’ve done this before but they don’t seem to think enough people care about it to bother changing anything.
Will do - It’s really astonishing that there isn’t a more flexible way of dealing with annotations.
Just thought you might be interested in some disappointing news - I have been using the beta of PDF Expert 7, and rather than improvements for those of exporting annotations, there are many steps backwards. The biggest is that they have taken away page numbers in the annotations export, basically making it useless for me for getting my highlights of readings into Drafts. Sigh.
Sorry to hear this. It would be nice if they at least let you customize this in the settings…
So I got PDF Expert 7. The new HTML only annotation summary is annoying, but it was fairly trivial to make an iOS Shortcut that takes this file as input from the share menu, converts it to text, and then creates a new Draft. (I also added a step to remove line breaks because I don’t want them.) I’m left with an HTML file I don’t need, but in some ways it is actually easier than trying to do “select all” from an email message…
I am trying to figure out how to use Find & Replace with RegEx to manipulate text. In this case, want to set a variable using RegEx and then replace text with the variable.
Mr. John Smith Mr. XX goes to town.
// define regex to use... const findRegex = /([M][r-s]\.\s)(\w*\s)(\w*)/g; // define replacement expression... const FirstName = "$2"; const LastName = "$3"; //Find Text const XX = "XX"; // // do the replacement... draft.content = draft.content.replace(XX, LastName); draft.update();
Mr. John Smith Mr. $3 goes to town.
What am I doing wrong?
I would suggest posting this sort of thing in a new topic in future, but looking at you code, you set-up a regex string and never use it. Maybe swap it in for your xx variable in your replace?
Try this: RegEx Factory - Drafts Action