PDA

View Full Version : Lua string.find() question



Varsuuk
May 11th, 2021, 17:15
I recall reading up on Lua string functions but oddly, I never noticed the discrepancy until this morning.

I noticed that the return values from string.find() were being assigned to 4 variables. Looked again and saw it is indicated (in the search results I hit) that find() returns start and end of match only. Two values. All the examples show this and how to then user string.sub() to get the contents of the match.

But obviously, if you print the following:
Debug.console(string.find(
sParams, "([^%s]+)%s*(.*)"))

You can see (assuming match) something like:
[5/11/2021 12:10:02 PM] #1|#11|s'1d6-1|'s'd6-1'

Which seems to imply the return values are: start, end, string being searched, matching part of the search (normally retrieved with sub())

Now, that is fine (and greatly convenient) and I should move on ;) but I just wanted to know where I SHOULD be looking to get right Lua documentation then because googling is steering me wrong ;) in at least this situation and maybe more I didn't know about.

superteddy57
May 11th, 2021, 17:19
Can use this:
https://www.lua.org/pil/20.1.html

Or this:
https://riptutorial.com/lua/example/20535/string-find--introduction-

In terms of pattern matching any lua reference site cover it in one way or the other. It's not the easiest, so you might have to tinker.

Varsuuk
May 11th, 2021, 18:13
That’s my point though ;)

That was the exact website and page I was reading. It declares that find returns two values as I said.



The basic use of string.find is to search for a pattern inside a given string, called the subject string. The function returns the position where it found the pattern or nil if it could not find it. The simplest form of a pattern is a word, which matches only a copy of itself. For instance, the pattern 'hello' will search for the substring "hello" inside the subject string. When find finds its pattern, it returns two values: the index where the match begins and the index where the match ends.

s = "hello world"
i, j = string.find(s, "hello")
print(i, j) --> 1 5
print(string.sub(s, i, j)) --> hello
print(string.find(s, "world")) --> 7 11
i, j = string.find(s, "l")
print(i, j) --> 3 3
print(string.find(s, "lll")) --> nil
When a match succeeds, a string.sub of the values returned by string.find would return the part of the subject string that matched the pattern. (For simple patterns, this is the pattern itself.)


Unless I misread it but the prints they show return only 2 values. Is it instead because I did Debug.console() that adds stuff?


I’m not concerned with pattern matching here. Just return values. I get how this works and it being a limited Tegel of sorts etc. Butvreading that page, I expect only the return of “#1 #11”

Trenloe
May 11th, 2021, 18:16
You've added some string captures into the pattern - anything between non-escaped brackets are returned (if there's a match). You have two sets of (...) in there - which is returning an additional variable for each.

superteddy57
May 11th, 2021, 18:18
The first two values of the return are the start and end positions. Any continued returned values would depend on your pattern matching. So yes, you should be concerned with pattern matching as this is all part of that mechanism.

Varsuuk
May 11th, 2021, 18:32
THAT'S IT!

Thanks man - I was just editing something and looked up find to see order of args and aw just 2 args... it didn't occur to me that it was capturing anything because I just looked at it as a matching pattern without looking at details. I was editing existing code to add some optional params for saving throws / half/none etc when I misinterpreted this.

I figured I was either looking at an older wiki than the version we used or I was misreading it but I couldn't see what I was misreading... because.I.didn't.READ.it.really obviously. :)

Varsuuk
May 11th, 2021, 18:38
The first two values of the return are the start and end positions. Any continued returned values would depend on your pattern matching. So yes, you should be concerned with pattern matching as this is all part of that mechanism.


I was responding to Trenloe before this one showed up - you are 100% correct. It was just that it was obvious to you that there was a capture. I have used captures all over (hate writing regex... makes head hurt lol as you intimated may be the case for some) but I stupidly did not read the details of the match string. I didn't even notice ()s ... monumental oversight especially since I am aware and used captures. By adding a capture it was not longer a simple "find()"

I think part of my problem is C++ (and Java, I guess, now...) to scripting change. Functions can be a bit more fluid than in those others (especially pre tuples which while I am familiar with, 99.9% of all I did was C++98/03

TOTAL D'OH moment for me :) thanks all for your patience.

damned
May 11th, 2021, 23:23
also Lua does not use regex syntax. close but different.

Varsuuk
May 12th, 2021, 03:51
Oh yeah, I know - which only makes it worse for me since I only barely know regex (now see what “tegal” was, the phone spell checking my regex…) and when in past months I was working on new thac0 I ran not issues ;) some things it was among the unsupported features if I recall.

Tgg he aņos to you guys, I realize what I misunderstood and my “huh” became /blush 8)