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

Reply
 
Thread Tools Display Modes
Old 03-09-2019, 06:26 PM   #1
kartalex
Human being with feelings
 
Join Date: Dec 2015
Posts: 172
Default Help!!! LUA: how to know if a string contains substring with special symbols?

Hi, all!

why this code:

p="PS02 - Fretted stereo2stereo (x86)"
s="PS02 - "
if string.match(p,s) then
reaper.ShowConsoleMsg("Yes!")
end

gives us "Yes!"

But this Code:

-- ex
p="PS02 - Fretted stereo2stereo (x86)"
s="PS02 - F"
if string.match(p,s) then
reaper.ShowConsoleMsg("Yes!")
end

gives us nothing??

How to know if some string contain another (with whitespaces or another symbols like "-" or "()" ???
kartalex is offline   Reply With Quote
Old 03-09-2019, 08:23 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,964
Default

The dash is a metacharacter that matches the previous character class zero or more times (non-greedily). You can escape it (and any other Lua pattern special characters) by prefixing with a % character.

https://www.lua.org/manual/5.3/manual.html#6.4.1

("PS02 - Fretted stereo2stereo (x86)"):match("PS02 - F") matches PS02 followed by zero or more spaces, a space then an F.
("PS02 - Fretted stereo2stereo (x86)"):match("PS02 %- F") matches PS02 followed by a space, a dash, a space and an F.
("PS02 - Fretted stereo2stereo (x86)"):find("PS02 - F", 1, true) does the same as the above. find's third argument enables plain text mode.

Last edited by cfillion; 03-09-2019 at 08:31 PM.
cfillion is offline   Reply With Quote
Old 03-09-2019, 11:01 PM   #3
kartalex
Human being with feelings
 
Join Date: Dec 2015
Posts: 172
Default

Quote:
Originally Posted by cfillion View Post
The dash is a metacharacter that matches the previous character class zero or more times (non-greedily). You can escape it (and any other Lua pattern special characters) by prefixing with a % character.

https://www.lua.org/manual/5.3/manual.html#6.4.1

("PS02 - Fretted stereo2stereo (x86)"):match("PS02 - F") matches PS02 followed by zero or more spaces, a space then an F.
("PS02 - Fretted stereo2stereo (x86)"):match("PS02 %- F") matches PS02 followed by a space, a dash, a space and an F.
("PS02 - Fretted stereo2stereo (x86)"):find("PS02 - F", 1, true) does the same as the above. find's third argument enables plain text mode.
Yes! It works!!
Thank you! I've spent the whole night, trying to find out how to do it!
kartalex is offline   Reply With Quote
Old 03-11-2019, 07:24 AM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,628
Default

Just in case you don't know of it, but there's a reference-manual for Lua as well:

https://www.lua.org/manual/5.3/manual.html#6.4.1

Quote:
%x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters. Any non-alphanumeric character (including all punctuation characters, even the non-magical) can be preceded by a '%' when used to represent itself in a pattern.
Means, such characters are called "magic characters" in Lua. There's a list in that chapter of all magic characters, that you might want to search for as well, that could do special stuff.
If you don't want them to do anything special, the % before them escapes them quite good.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is online now   Reply With Quote
Old 03-12-2019, 01:40 PM   #5
kartalex
Human being with feelings
 
Join Date: Dec 2015
Posts: 172
Default

Quote:
Originally Posted by mespotine View Post
Means, such characters are called "magic characters" in Lua. There's a list in that chapter of all magic characters, that you might want to search for as well, that could do special stuff.
If you don't want them to do anything special, the % before them escapes them quite good.
Yes, now I understand how it works, thanks!
kartalex 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 03:12 AM.


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