RPTools.net

Discussion and Support

Skip to content

It is currently Fri Nov 17, 2017 12:35 pm 






Reply to topic  [ 5 posts ] 

Previous topic | Next topic 

  Print view

Author Message
User avatar  Offline
Dragon
 
Joined: Mon Feb 09, 2009 10:53 am
Posts: 419
 Post subject: Regex help
PostPosted: Fri Nov 10, 2017 12:37 pm 
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..."


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7947
Location: Bay Area
 Post subject: Re: Regex help
PostPosted: Fri Nov 10, 2017 1:19 pm 
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:
[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:
<!-- 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:
[: regex = "(Item: [^-]+- ).*?(Price:.*?gp)"]


That will capture each pair of Item/Price combos saving only text wanted in group 1 and 2.

Code:
[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:


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Mon Feb 09, 2009 10:53 am
Posts: 419
 Post subject: Re: Regex help
PostPosted: Fri Nov 10, 2017 2:01 pm 
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..."


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Mon Feb 09, 2009 10:53 am
Posts: 419
 Post subject: Re: Regex help
PostPosted: Sat Nov 11, 2017 9:12 pm 
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..."


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Nov 10, 2009 6:11 pm
Posts: 7947
Location: Bay Area
 Post subject: Re: Regex help
PostPosted: Sat Nov 11, 2017 10:35 pm 
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:


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 5 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:

Who is online

In total there are 2 users online :: 0 registered, 0 hidden and 2 guests (based on users active over the past 5 minutes)
Most users ever online was 243 on Sun Nov 04, 2012 6:14 am

Users browsing this forum: No registered users and 2 guests





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman