Regex with positive look ahead and end of text problem

Talk about whatever topic you'd like, RPG related or not. (But please discuss things related to our software in the Tools section, below.)

Moderators: dorpond, trevor, Azhrei

Post Reply
User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

Regex with positive look ahead and end of text problem

Post 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. https://regexr.com/4s63o

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
Last edited by AriesBreath on Tue Jan 14, 2020 8:54 am, edited 1 time in total.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

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

Post 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: http://lmwcs.com/rptools/wiki/getGroup

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.

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

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

Post 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

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

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

Post 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...

User avatar
AriesBreath
Cave Troll
Posts: 32
Joined: Thu May 02, 2019 10:37 am

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

Post 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

Post Reply

Return to “General Discussion”