Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 06-01-2020, 04:53 PM   #1
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default Lua: Help with capturing part of a string with magic patterns

Hey guys,

I'm unable to figure out a way using magic characters in LUA to ignore everything between the second and last underscore.

Example... I have this:

Code:
Inst_1A_8ch_Sus_Leg_idx001_sn_056_en_055
And I want the result to be:

Code:
Inst_1A_055
I was hoping to use some sort of form of string.sub(filename, filename:match(".*()/")+1)) type of code to do it for me. Anyone know what the proper pattern would be?

Thanks.
__________________
Cheers... Andrew K
Reaper v6.80+dev0621 - June 21 2023 • Catalina • Mac Mini 2020 6 core i7 • 64GB RAM • OS: Catalina • 4K monitor • RME RayDAT card with Sync Card and extended Light Pipe.

Last edited by Thonex; 06-01-2020 at 05:00 PM.
Thonex is offline   Reply With Quote
Old 06-01-2020, 05:32 PM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

You can use string.match with multiple captures, like in this example, where the parts of the string of the first (.-_.-_) and the second (.*) are stored in B and C respectively:

Code:
A="Inst_1A_8ch_Sus_Leg_idx001_sn_056_en_055"

B,C=A:match("(.-_.-_).*_(.*)")
D=B..C
The first capture (.-_.-_) says:
Take everything from beginning to the first _ and then between the first _ to the second _

inbetween the captures, it shall ignore everthing until the last _

the last capture (.*) says
take everything fom after the last _ til the end.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 06-01-2020, 05:46 PM   #3
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
You can use string.match with multiple captures, like in this example, where the parts of the string of the first (.-_.-_) and the second (.*) are stored in B and C respectively:

Code:
A="Inst_1A_8ch_Sus_Leg_idx001_sn_056_en_055"

B,C=A:match("(.-_.-_).*_(.*)")
D=B..C
The first capture (.-_.-_) says:
Take everything from beginning to the first _ and then between the first _ to the second _

inbetween the captures, it shall ignore everthing until the last _

the last capture (.*) says
take everything fom after the last _ til the end.
Brilliant!!!

Thanks so much Mespotine!!! I really will try to digest this... as I'm needing manipulate strings more and more.

Maybe I should learn Klingon to help 😀

Thanks again!!
__________________
Cheers... Andrew K
Reaper v6.80+dev0621 - June 21 2023 • Catalina • Mac Mini 2020 6 core i7 • 64GB RAM • OS: Catalina • 4K monitor • RME RayDAT card with Sync Card and extended Light Pipe.
Thonex is offline   Reply With Quote
Old 06-01-2020, 06:09 PM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

The most important things are

.- and .* as well as captures () and the knowledge, that magic characters in your string can prevent proper pattern-matching, so they need to be escape with a % before them.

for instance

%.%* will look for the characters .* in a string while .* will look for the whole string and returning the whole string because of that.

If you get that, then most of pattern matching can be done.

The rest is sugar for specific usecases.


Just toy around with simple patterns and try to make the matching work. If you got a sense for them, you can work your way up from there.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 06-02-2020, 01:08 AM   #5
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,721
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
The most important things are

.- and .* as well as captures () and the knowledge, that magic characters in your string can prevent proper pattern-matching, so they need to be escape with a % before them.

for instance

%.%* will look for the characters .* in a string while .* will look for the whole string and returning the whole string because of that.

If you get that, then most of pattern matching can be done.

The rest is sugar for specific usecases.


Just toy around with simple patterns and try to make the matching work. If you got a sense for them, you can work your way up from there.
Thanks for the very clear and detailed explanation!!

That actually helps me a lot!

Cheers,

Andrew K
__________________
Cheers... Andrew K
Reaper v6.80+dev0621 - June 21 2023 • Catalina • Mac Mini 2020 6 core i7 • 64GB RAM • OS: Catalina • 4K monitor • RME RayDAT card with Sync Card and extended Light Pipe.
Thonex is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:07 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.