Extract data from a block of text to populate variables.

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

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

Re: Extract data from a block of text to populate variables.

Post by wolph42 »

for regex i would use:

Code: Select all

:(.*?)[+]
where (.*?) group 1 is and thus everything between the : and the +. The ? is 'non greedy' without it it just matches everything after the : so also the + other stuff.

Shadow Wizard
Giant
Posts: 183
Joined: Mon Apr 11, 2011 8:11 pm

Re: Extract data from a block of text to populate variables.

Post by Shadow Wizard »

I just got this to work today! Thank you very much! On a different macro, but still. I am going to be able to do a lot more with this!

Shadow Wizard
Giant
Posts: 183
Joined: Mon Apr 11, 2011 8:11 pm

Re: Extract data from a block of text to populate variables.

Post by Shadow Wizard »

Okay, maybe I don't have it....
Here are examples 4 possible inputs:
Attack: Glaive +9 melee (1d10+3 plus infernal wound) or claw +8 melee (1d6+2)
Attack: Great Big Glaive +9 melee (1d10+3) or claw +8 melee (1d6+2)
Attack: Glaive +9 melee (1d10+3 plus infernal wound)
Attack: Glaive +9 melee (1d10+3)

I need to return "9" and "1d10+3"
Basically the first number, and the first formula.
This is what I have:
[H: id = strfind(input,"Attack: ([A-z]+)([+-]*[0-9]+)[A-z]*\\(+([+-]*[0-9]*[d]*[0-9]*[+-]*[0-9]*?)")]

I am able to return the "9" But not the "1d10+3"
And I have been fighting for hours. Please help.
** Actually I need to correct something. I had replaced all the spaces with "QQ" in order to try and parse it. So in the above examples all the spaces are replaced with QQ. That being said, I don't care if we use the original (with the spaces) or the modified (with the QQ's) to parse out the data.

Longshot
Giant
Posts: 117
Joined: Tue Feb 22, 2011 8:59 am
Location: Honolulu, Hi.

Re: Extract data from a block of text to populate variables.

Post by Longshot »

This may be a few months old, but if it helps:

Code: Select all

[h: sample1 ="Attack: Glaive +9 melee (1d10+3 plus infernal wound) or claw +8 melee (1d6+2)"]
[h: sample2 ="Attack: Great Big Glaive +9 melee (1d10+3) or claw +8 melee (1d6+2)"]
[h: sample3 ="Attack: Glaive +9 melee (1d10+3 plus infernal wound)"]
[h: sample4 ="Attack: Glaive +9 melee (1d10+3)"]
[h: sample5 ="Attack: Glaive +9 melee (1d10 plus infernal wound)"]

[h:regEx=strformat('[+-](\\d+).*?\\((\\d+d\\d+[+-]?\\d?)')]

[H: id = strfind(sample1,regEx)]
[Output1 = "To Hit:"+getGroup(id,1,1)+" Damage:"+getGroup(id,1,2)+"<br/>"]

[H: id = strfind(sample2,regEx)]
[Output1 = "To Hit:"+getGroup(id,1,1)+" Damage:"+getGroup(id,1,2)+"<br/>"]

[H: id = strfind(sample3,regEx)]
[Output1 = "To Hit:"+getGroup(id,1,1)+" Damage:"+getGroup(id,1,2)+"<br/>"]

[H: id = strfind(sample4,regEx)]
[Output1 = "To Hit:"+getGroup(id,1,1)+" Damage:"+getGroup(id,1,2)+"<br/>"]

[H: id = strfind(sample5,regEx)]
[Output1 = "To Hit:"+getGroup(id,1,1)+" Damage:"+getGroup(id,1,2)+"<br/>"]
Results:
To Hit:9 Damage:1d10+3
To Hit:9 Damage:1d10+3
To Hit:9 Damage:1d10+3
To Hit:9 Damage:1d10+3
To Hit:9 Damage:1d10

The "\\d" means digit, so \\d+ is "One or more digits".
Also, maybe you'll have to parse "1d10+3", maybe just "1d10".
So, "\\d+d\\d+" will match all digits before a letter "d" and after. If it's "10d10", you're covered.
After 1d10, you MAY have a "+1, or -1" or +11 or -13, etc. Or nothing. So we say [+-]? meaning maybe a "+", maybe a "-".
Finally \\d+? which means "maybe a series of one or more digits".

Capturing what you want:
As far as capturing what you want, there are two sets of "( )" that are not escaped (meaning the preceding "\\"). The "\\(" ones mean looks for an actual "(" in the imported text. The parenthesis without preceding "\\" are the ones that say, "Grab what's between "(...)". I made two pairs: the pair wrapping the +9, and the one wrapping the 1d10+3.
When I get groups, I say getGroup(id,1,1) for the first "(...)", and getGroup(id,1,2) for the second.

What will break this search? Well, if damage is, like, just "8" -no roll. It NEEDS a "d" to work, as in "#d#".
To keep things from breaking, I tend to protect myself with an "if" such as this:

Code: Select all

[h: if(getFindCount(id)>0):getGroup(id,1,1)]


Also, if you use getGroup(id,1,0), you get the whole line. It's like the Group "(...)" wraps the whole search from the first character you matched to the last.

Hope that helps.

Post Reply

Return to “Macros”