String matching is ugly, how to do that

Hi,

I want to simply match a string in which only one or two numbers might change. Examples:

Cutoff 1
Cutoff 2
...
Frequency 1 (EQ 1)
Frequency 2 (EQ 2)
...

Now I am trying to use string.match(), and finding some good documentation, but I only can find stupid dissertations about lua string matching, not giving any real life examples.

I tried this:

Frequency [%d+] (EQ [%d+])
Frequency %d+ (EQ %d+)

Both tries for a proper matching string do not work, of course. Any idea here, please? Questions:

  • How do I match an exact string part, like "Frequency " (including the whitespace)?
  • Do I need to interpolate the bracket signs?

Omg, I simply wanted to use a simple pattern matching, but it turns out that LUA devs expect me to spend my whole weekend for such stupid things…

You need to escape the brackets ( ). This should work:

Use ^ and $ for exact matching:

Infos about:

See: Programming in Lua : 20.2

2 Likes

Thanks! Right, its called “escaping”, not “interpolating” hehe. Let’s try that…

EDIT: See, that worked. You should write the LUA docs.

2 Likes

String stuff gets annoying. I’ve been gradually accumulating helper functions in a library I use in most of my tools to do stuff like splitting on spaces, certain matches, turning a comma-delimited string into a table, etc.

1 Like

@toimp Now I would like to match a string like:

startString + any kind of string

I tried then:

string.match(myString, "^" .. startString .. "[.]*")
string.match(myString, "^" .. startString .. "(.*)")

But those do not work. Any clue?

Not sure but if you wanted to match:

startString hello

or

startString1234

or

startString

I’d probably try:

string.match("startString hello","^startString.*")

That may match so long as it starts with the “startString” characters(?)

Hi,

it’s startStringAnything, and startString can consist of any character (except the magic ones). Tried your suggestion, but does not work.

As I say, I’m not sure from the info. Sorry to have wasted your time on my utterly fucking stupid retarded post.

What? No, thanks a lot for traying to help!

Yeah my description is lame. Let me try again. I have a variable startString, containing for example “!!”. Now I want to match some string, if my startString is at the beginning of that string.

I tried your suggested pattern. But somehow it doesn’t match.

Do you have an exact code example? Both of your examples should work, but it depends what value startString has.

myString = "my little test works!"
startString = "my little "

if string.match(myString, "^" .. startString .. ".*") then
  print ("match")
else
  print ("no match")
end

It’s important that startString should be escaped, when the value has magic characters.

Btw. just play around here:
https://www.lua.org/cgi-bin/demo

Damn! Guys, @4tey @toimp so sorry for wasting your time! :sweat_smile: The string I wanted to match simply was the wrong one, I looked for “device.name”, which never changes, instead “device.display_name”… LOL