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.
Regex help
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Regex help
"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..."
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."
Re: Regex help
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:
The above assumes you're looping through each line. One way to do that is:
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:
That will capture each pair of Item/Price combos saving only text wanted in group 1 and 2.
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 = ""]
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)]
}]
Code: Select all
[: regex = "(Item: [^-]+- ).*?(Price:.*?gp)"]
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)]
}]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Regex help
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).
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..."
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."
Re: Regex help
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..."
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."
Re: Regex help
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools