Regex help

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

Post Reply
User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Regex help

Post by Irrlicht »

I know there's a RegEx thread just below, but I didn't want to invade someone else's thread.

I need a bit of help, since I'm getting confused with regex here...
I have magic items stat blocks, from which I only want to extract name and price. Possibly, with multiple items at once, not just one each time.

Sample original text:
Item: Cloak of Invisibility - slot: shoulders - CL 7th
Description: This cloak [...] times per day.
Creation cost: 1,250 gp - Price: 2,500 gp - Requirements: Craft Wondrous Items, Invisibility

Item: Ring of Protection - slot: finger - CL 5th
Description: This ring [...] times per day.
Creation cost: 750 gp - Price: 1,500 gp - Requirements: Forge Rings, Mage Armor


Wanted result:
Item: Cloak of Invisibility - Price: 2,500 gp
Item: Ring of Protection - Price: 1,500 gp


All I could come up with in Regexr.com could at best get the name and slot till to the second " - " (and I don't want the slot and the second " - ").

Any help? Thanks in advance.
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Regex help

Post by aliasmask »

How are you entering the text to regex'd? There are many ways to do so. You can use an input text which does not allow CRLF, you can use input textarea which will require some tweaks to convert some characters. You can just put it in quotes in a variable string or you can put it all in a lone macro and use getCommand to get the data.

The regex is pretty simple, but it depends on how the data is being read in to implement. For example:

Code: Select all

[H: id = strfind(line,"^Item:([^-]+)")]
[H, if(getFindCount(id)): item = trim(getGroup(id,1,1)); item = ""]
The above assumes you're looping through each line. One way to do that is:

Code: Select all

<!-- put each line of raw data in to a json array. -->
[H: EOL = decode("%0A")]
[H: buttonIndex = getMacroIndexes("Items")]
[H: lines = json.fromList(getMacroCommand(buttonIndex),EOL)]

[H: itemsFound = "[]"]
[H, foreach(line,lines), code: {
   [H: id = strfind(line,"^Item:([^-]+)")]
   [H, if(getFindCount(id)): item = trim(getGroup(id,1,1)); item = ""]
   [H, if(! json.isEmpty(item)): itemsFound = json.append(itemsFound,item)]
}]
This done by putting all your text in to a macro called "Items" and running this macro. You can output the json or save it. Since price is on a different line, then you'll have to wait for the next price line and do something similar. You can also read it in as one big chunk and do this:

Code: Select all

[: regex = "(Item: [^-]+- ).*?(Price:.*?gp)"]
That will capture each pair of Item/Price combos saving only text wanted in group 1 and 2.

Code: Select all

[H: numItems = getFindCount(id)]
[H, for(i,1,numItems+1), code: {
   [H: part1 = getGroup(id,i,1)]
   [H: part2 = getGroup(id,i,2)]
   [H: itemsFound = json.append(itemsFound,part1+part2)]
}]

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Regex help

Post by Irrlicht »

I'm on the run now, but at a quick glance, I think the latter is most akin to what I need.

I didn't intend to use inputs, but to get the text into a variable (actually, pasting the text in a token's notes and retrieving those token notes).
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Regex help

Post by Irrlicht »

Uhm, I'm unable to make it work, using a variable in which token notes are stored as mentioned above...
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Regex help

Post by aliasmask »

The last method only works if you enter you text via an input text. That way it strips all the CRLF for you. You can also delete the CRLF characters from your input before running though regex.

Post Reply

Return to “Macros”