Find values between markers?

Hey guys,

In all my years of programming, regular expressions have just been the bane of my existence, but sadly my knowledge of how they function just isn't enough to solve my current problem. Basically I'm looking to set up some escape-string functionality, like that found in the message window. For example, I have:

\IDENTIFIER[KEY: VALUE]

Where IDENTIFIER is a string with more than one character, I had it working using:

/\\IDENTIFIER\[(\w+):[ ](\w+)\]/Though my test case only had my code in the message window. Once I added some other text, it just broke. It also broke when I made VALUE be more than one word, which is expected I guess because of the second capture group (\w+). I tried using (.+) to cater for that but that just matches the entire message block.

Any of you wizards have any idea how to get this to work? I know the Yanfly plugins use similar regex statements but seemingly nothing I can one-to-one against to see where I'm going wrong.
If KEY is always one word, but VALUE can be multiple words, your regex is correct apart from the second capture group as you expected. The fixed regex would be like this:

/\\IDENTIFIER\[(\w+):[ ](.*?)\]/I also suggest using a case insensitive modifier on your regex so you can type \identifier as well as \IDENTIFIER to be less strict.

The (.*?) capture group is a lazy capture (as opposed to greedy), where it matches all characters except newline.

As for it breaking when you add more text to the message, that's likely a code flaw, as there's nothing wrong with capturing in this regex.

Hope this explains it well enough for you.
Thanks so much, been pulling my hair out over this. Looks like once I changed it to lazy it doesn't break with the other text, though might've inadvertently fixed something else in the code. Also, I am using 'g' and 'i' flags for the regex 🙂

Original thread: https://forums.rpgmakerweb.com/threads/find-values-between-markers.54685/