Need help on how to approach regex

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Hi,
I am working on a plugin and it's progress well but I am struggling on the part which is : Notetag using Regex.

I tried to look at tutorial but I don't seem to understand how it work...

so could I get help in understanding how to approach a specific regex?

in simple my regexp is literally this :
Code:
<Prices: [copper: 10, silver: 1, gold: 0 ]> // when a prices is ommited it is fine
//so far this is how my regex look
<\s*prices\s*:\s*([0-9]*)\s*>
so I want to try to implement that (btw if you think the '[' are to much it can be removed.
I just don't know how to approach I tried to create it but I can't get an idea on how to approach that.

thanks for any of your help!
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,695
Reaction score
1,113
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi there!
I think the best way you can see if your regex is working the way you want it, is using this site:
https://regex101.com/
You can test it there.

[OFF]
I know regex can make things much easier and simple in the code, once you can make it work.
But, personally, I only use regex when it comes to search in a large text since regex can have a lot of things to take into consideration if you are worried about performance.
Maybe you could try a different approach to get these values? Like, use the slice or substring in the string.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
It might be simpler to have 3 regular expressions, one for each of the currency types.

Of the top of my head maybe something that looks a bit like this
/<[^>]*prices?[^>]*copper[^>]*([0-9]+)[^>]*>/im
/<[^>]*prices?[^>]*silver[^>]*([0-9]+)[^>]*>/im
/<[^>]*prices?[^>]*gold[^>]*([0-9+)[^>]*>/im

*hugs*
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,248
Reaction score
1,250
First Language
Spanish
Primarily Uses
RMVXA
do you want to get whatever comes after "prices" as it comes, or do you want them to be ordered in an array?
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
Hi there!
I think the best way you can see if your regex is working the way you want it, is using this site:
https://regex101.com/
You can test it there.

[OFF]
I know regex can make things much easier and simple in the code, once you can make it work.
But, personally, I only use regex when it comes to search in a large text since regex can have a lot of things to take into consideration if you are worried about performance.
Maybe you could try a different approach to get these values? Like, use the slice or substring in the string.
I see yeah it's just I want to simplify the job of the user.

It might be simpler to have 3 regular expressions, one for each of the currency types.

Of the top of my head maybe something that looks a bit like this
/<[^>]*prices?[^>]*copper[^>]*([0-9]+)[^>]*>/im
/<[^>]*prices?[^>]*silver[^>]*([0-9]+)[^>]*>/im
/<[^>]*prices?[^>]*gold[^>]*([0-9+)[^>]*>/im

*hugs*
I see your point zeriab the problem is the currency can change (it can be any type of currency and any type of number this is where it get complicated

do you want to get whatever comes after "prices" as it comes, or do you want them to be ordered in an array?
Hum I want to be able to extract the array but I can do that by using .match + the "copper, etc" is an ID (which can be anything the user decides to use)

so if the user wants he can make 10 currencies I want the user to be able to implement those in notetags
i.e : diamonds: 10, iron: 10 , etc

pretty much what I need to extract is an array with a struct like that :

[{id: "string" , value: 10 }, etc] (I can easily create the struct outside of the regexp tho)
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,091
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
Note that database objects have their noteboxes checked on game load (using a regular expression) for notetags in the form <name:value>. Objects with such tags have the values added as sub-properties to their meta property. So if your notetag was on item ID 1 in the database, you could check it by doing:
JavaScript:
console.log($dataItems[1].meta.Prices);
// "copper: 10, silver: 1, gold: 0"
So you could use that as a starting point for your parsing~ :kaophew:

This regular expression should match any text-colon-integer type string, e.g. "copper:10":
JavaScript:
/,?\s*(.+?):\s*(\d+)\s*/g
You can then extract the matches as required, e.g.
JavaScript:
var rgx = /,?\s*(.+?):\s*(\d+)\s*/g;
var tag = "copper:10, silver: 1,gold:   0";  // for testing
var res = [], label, num, match;
do {
  match = rgx.exec(tag);
  if (match) {
    label = match[1], num = parseInt(match[2], 10);
    res.push({name:label,value:num});
  }
} while (match);
console.table(res);
(There are probably other approaches.) Try sticking it in the console (F8 during playtest) if you want to try it out! Since this expression has the g flag, it is global, i.e. repeated matches will progress through the string. :)

A distinction worth pointing out:
  • ,? means "0 or 1 comma".
  • (.+?) captures the shortest admissible string of characters for that part of the expression. The ? here means "non-greedy match".
Technical details on regular expressions, in case you're interested:
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
well thank you guys for the help! and thanks Tea's for the link I will use it :3!
@LTN Games I will use your approach (while creditting you of courses)
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
533
Reaction score
235
First Language
English
Primarily Uses
RMMV
I wouldn't use regex for this, actually. I'd use String.split. Something like the following:

JavaScript:
$dataItems[1].meta.Prices.split(',').map(s => s.trim().split(':').map(s => s.trim())).map(arr => ({type: arr[0], amount: parseInt(arr[1])}))
Then you write your note tag like this instead of what you posted:
<Prices: copper: 10, silver: 1, gold: 0 >

Explanation of what that code actually does:

  1. .split(',') - Pretty self-explanatory. Splits the string into an array by commas.
  2. .map(s => ... ) - This takes the array on the left and applies the code ... to every element to produce a new array.
  3. .trim() - Remove spaces from the beginning and end of a string
So basically it splits on comma and trims spaces so that you have ["copper: 10", "silver: 1", "gold: 0"]. Then it splits each of those on colon so now you have [['copper', '10'], ['silver', '1'], ['gold', '0']]. Finally, it turns those subarrays into an object for easier access, so now you have [{type:'copper',amount:10}, {type:'silver', amount:1}, {type:'gold', amount:0}].

Here's the code again broken down onto multiple lines, which might help you see what it's doing:

JavaScript:
$dataItems[1].meta.Prices
.split(',')
.map(s => 
  s.trim()
  .split(':')
  .map(s => s.trim())
)
.map(arr => ({
  type: arr[0],
  amount: parseInt(arr[1])
}))
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,085
Members
137,584
Latest member
Faustus2501
Top