PDA

View Full Version : String.match return multiple text even if not all exist



MadBeardMan
April 2nd, 2020, 14:26
Folks,

Never liked regex but I used it, though Lua doesn't use regex, it uses pattern matching

Inside Traveller when you drop a skill onto an existing skill it increases that Skill by 1.

But specialised skills don't work.

For example:

Melee (unarmed) 1, Carouse 2

I have it making Carouse 3 if Carouse is dragged over the NPC skills, using


sSkill, nExistingLevel = string.match(sSkillsCurrently, "(" .. sNewSkill .. ") (%d*)");

But what I want is for it to also now return the specialism if it exists, just can't seem to get it to work


sSkill, sSpecialism, nExistingLevel = string.match(sSkillsCurrently, "(" .. sNewSkill .. ") (%s+) (%d*)");

Returns nothing but nils.

Cheers,
MBM

Trenloe
April 2nd, 2020, 14:40
I tend to test for the string with the word in brackets first and then if there isn't a match try for a match without brackets.

To check for three captures, with the middle one being a single word consisting of alphanumberic characters within brackets, try this:


"(" .. sNewSkill .. ") %((%w+)%) (%d*)"

If the specialization could be more than one word, use:


"(" .. sNewSkill .. ") %([(%w%s]+)%) (%d*)"

Then if you don't get anything, just go for two captures without the specialization in brackets:


"(" .. sNewSkill .. ") (%d*)"

I'm sure it possible to do it all in one regular expression, but I'm not at that level of know how!

MadBeardMan
April 2nd, 2020, 17:25
Hi Chap,

Super, thanks for this. Works nicely. The specialisation would only be single, so 'Gun Combat (slug)' or 'Drive (Wheeled)'.

Hope you and yours are safe and sound wherever in the world you are!

Cheers,
MBM

MadBeardMan
April 2nd, 2020, 17:55
Hi Trenloe,

Maybe you can answer this one then.


Runtime Notice: s'sSkillsCurrently' | s'Medic 2, Melee (unarmed) 0, Pilot 0'
Runtime Notice: s'sOldSkillLevel' | s'Melee (unarmed) 0'
Runtime Notice: s'sNewSkillLevel' | s'Melee (unarmed) 1'
Runtime Notice: s'sNewSkills' | s'Medic 2, Melee (unarmed) 0, Pilot 0'

It's not updating the skill level with the new skill level, this is the code:


Debug.console('sOldSkillLevel', sOldSkillLevel);
Debug.console('sNewSkillLevel', sNewSkillLevel);
sNewSkills = sSkillsCurrently:gsub(sOldSkillLevel, sNewSkillLevel);
Debug.console('sNewSkills', sNewSkills);
DB.setValue(vTarget, "skills", "string", sNewSkills);

Any thoughts on why gsub is not working?

Cheers,
MBM

Trenloe
April 2nd, 2020, 18:42
Any thoughts on why gsub is not working?
The brackets are reserved characters in regular expressions - you'll need to escape those in the strings used in string.gsub.

This is something I've done recently for Pathfinder 2 automation development (still in testing for those who are interested):


-- Replace reserved Regex characters with escaped characters
sSearchAutomationString = string.gsub(sAutomationString, "%+", "%%%+");
sSearchAutomationString = string.gsub(sSearchAutomationString, "%-", "%%%-");
sSearchAutomationString = string.gsub(sSearchAutomationString, "%(", "%%%(");
sSearchAutomationString = string.gsub(sSearchAutomationString, "%)", "%%%)")

P.S. Hanging in there mate. Hope you and your family are too. I may get down to see you when this is all over... unless I escape as fast as I can. ;)