Need help on how to approach regex

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!
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.
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*
do you want to get whatever comes after "prices" as it comes, or do you want them to be ordered in an array?
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)
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~ 😮‍💨

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.
Spoiler: Example
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:
thank you a lots yeah I wil test it out
Using what @caethyril has given you and with a method I've been using for a while, you can extract the data pretty easily. I made a code sandbox if you want to see it in action https://codesandbox.io/s/rpgmaker-mv-filtertext-wytqf?file=/src/index.js
@LTN Games Oh wait... you literally just did the solution for me XD??
This is another good place to learn/test regex
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)
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])
}))

Original thread: https://forums.rpgmakerweb.com/threads/need-help-on-how-to-approach-regex.123037/