Page 1 of 1

Regex with positive look ahead and end of text problem

Posted: Tue Jan 14, 2020 7:11 am
by AriesBreath
Hello again.
I'm experimenting with some macros using regex. Currently, I'm working on a macro that will get all the text in GM Notes (where I usually store the original character sheet) and select some parts of it to store them in the properties (DnD 5e). It's working great, until the "Actions" section and beyond, where it's hard to tell the regex where exactly are the Actions, Reactions, Legendary Actions etc.

The idea I'm trying to follow is: find the word "Actions" (positive look behind) and select everything (not greedy) until you find either the words "Reactions, Legendary Actions etc" (positive look ahead). This way, if a sheet is missing some of the actions (like the reactions), the reg ex keep selecting until it finds the next sets of actions and stops. It works, but the problem is that if a character only has the actions and nothing else the regex doesn't match anything.

Code: Select all

(?<=Actions.*\\n\\n)([\\s\\S])+?(?=\\nReactions|\\nLegendary Actions|\\nLair Actions|\\nRegional Effects)
To overcome this problem, I tried by adding a "$" as an alternative to the end of the positive look ahead. In a RegEx tester works BUT MapTool doesn't want to accept it, it says "java.lang.IllegalArgumentException: Illegal group reference".

Code: Select all

(?<=Actions.*\\n\\n)([\\s\\S])+?(?=\\nReactions|\\nLegendary Actions|\\nLair Actions|\\nRegional Effects|$)
Here is a link to the tester with a really big character sheet containing everything a sheet can contain and the regex code used to match until the very end. keep in mind you'll find the sets of actions without the final "s" to keep the regex from stopping at them, I was trying to match until the end.

How can I tell MapTool to match until the next set of actions OR until the end of the sheet if there are no other sets of actions?

EDIT: I thought of a workaround. I can create a new regex that will have to match every set of actions but "Actions". If the count is greater than 0 (aka if there are other sets of actions besides "Actions"), than the macro have to use the Regex I wrote before, otherwise, use the same regex but without the positive look ahead. Have to test it, but it's still a workaround instead of a real solution

Re: Regex with positive look ahead and end of text problem

Posted: Tue Jan 14, 2020 8:51 am
by wolph42
well, its been awhile, but positive lookaheads have always given me a headache (stronger headache than regex in itself provides) and I was really relieved when I learned to work with groups! I've extended the wiki article based on that as its way easier to deal with regex once you get the hang of that:

that however does not directly solve your issue. IRC you need to escape the $: \$ (or \\$ ??)

with groups you get (and this has been 2 years so my regex is really rusty) something like

(Actions\n\n)*(.*?)(Reaction\n\n)*(.*?)(Legendary Action\n\n)*(.*?)(Lair Action\n\n)*(.*?)(Regional Effect\n\n)*(.*?)

here I assume that (.*?) is non gready, didn't test this.

then groups 2,4,6,8 and 10 contain the strings related to 'action' 'reaction' etc.

Re: Regex with positive look ahead and end of text problem

Posted: Tue Jan 14, 2020 8:58 am
by AriesBreath
Thanks, but I already work with the groups. The problem is that I cannot make it to match until a certain word is found OR the end of the text if no word is found. By the way i tried with double escapes on the "$" but it doesn't work, also I don't think it have to be double escaped

Re: Regex with positive look ahead and end of text problem

Posted: Tue Jan 14, 2020 9:21 am
by wolph42
well I do recall creating a regex parser in my W40K framework where I did exactly what you try to do. You could have a look at that. IRC it was something called like 'parser' and on lib:ships or such. (sry been a while)

edit: link in sig. (under frameworks) the lib there is lib:Ships then the macros in the group 'regex parser' on lib:DH there's also a regex parser that was build to proces entire sheets. My approach is however quite different than yours so not sure it will help...

Re: Regex with positive look ahead and end of text problem

Posted: Wed Jan 15, 2020 6:43 am
by AriesBreath
I checked your framework, it's full of useful macros but I can't really understand what it's going on in there, so I preferred taking the workaround solution, that is working