CougarFFW04 Posted March 27, 2024 Posted March 27, 2024 (edited) Hi, I am looking for a lua pattern that would accept the following format exemples : Case 1 : "TEXT1 #i1 TEXT2 " or Case 2 : "TEXT1 #i1 TEXT2 i2 " or Case 3 : "TEXT1 #i1 TEXT2 i2 SEQ" Where TEXT1 could be any word , TEXT2 could be any wordS (possibly only one) separated by space SEQ is either #i3 or !#i3 i1, i2 and i3 are integers. The aditional condition would be that if i2 is NOT finishing the sequence then it MUST finish with SEQ (as defined above) I tried different patterns like "^%w+ #%d+( [A-Za-z]%w*)*( %d+( !?#%d+)?)?$" but it fails! What is wrong in my pattern ? The one suggested by chatGPT (in the code below) does not work either obj = "ROST1 #10 Hornet1 Blue 25 !#10" pattern="^%w+ #%d+ ([A-Za-z]%w+)( %d+( !#%d+)?)?( [A-Za-z]%w+)*$" if string.match(obj,pattern) then print("Good") else print("bad") end Anyone suggesting a good pattern ? You can test any sugestions at https://onecompiler.com/lua/ Thanks Edited March 27, 2024 by CougarFFW04
Grimes Posted March 27, 2024 Posted March 27, 2024 Patterns are not something I find to be all that fun. I assume you are wanting to use it as a command of sorts where a player enters it via chat or F10 mark and it uses the inputs to call a function? I use this to get each word added to a table, then I use the table itself to manipulate or check the data. commands[1] is the command being issued and commands[2] could be an optional value, if it doesn't exist then use some default value. local commands = {} for w in string.gmatch(event.text, "%w+") do table.insert(commands, w) end I suppose you could make a pattern for each of the 3 cases and use if string.match(obj, pattern1) or string.match(obj, pattern2) or string.match(obj, pattern3) then print("good") end The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
CougarFFW04 Posted March 30, 2024 Author Posted March 30, 2024 Hi Grimes, Thanks for your prompt reply and your tips. F10 commands is not really what it is supposed to do but anyway I have manage to fix it. Many thanks to take on your time to help, I am pretty sure your answer will help me or other guys sooner or later. Thanks again
Recommended Posts