STAR TREK 2d20
  1. #1

    String.match return multiple text even if not all exist

    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

    Code:
     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

    Code:
     sSkill, sSpecialism, nExistingLevel = string.match(sSkillsCurrently, "(" .. sNewSkill .. ") (%s+) (%d*)");
    Returns nothing but nils.

    Cheers,
    MBM
    Ruleset and much more content built for FGU.
    Come join me on Twitter at: https://twitter.com/MadBeardMan to see what I'm up to!

  2. #2
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,362
    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:

    Code:
    "(" .. sNewSkill .. ") %((%w+)%) (%d*)"
    If the specialization could be more than one word, use:

    Code:
    "(" .. sNewSkill .. ") %([(%w%s]+)%) (%d*)"
    Then if you don't get anything, just go for two captures without the specialization in brackets:

    Code:
    "(" .. 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!
    Last edited by Trenloe; April 2nd, 2020 at 14:43.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  3. #3
    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
    Ruleset and much more content built for FGU.
    Come join me on Twitter at: https://twitter.com/MadBeardMan to see what I'm up to!

  4. #4
    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:

    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
    Ruleset and much more content built for FGU.
    Come join me on Twitter at: https://twitter.com/MadBeardMan to see what I'm up to!

  5. #5
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,362
    Quote Originally Posted by MadBeardMan View Post
    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):

    Code:
    			-- 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.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
FG Spreadshirt Swag

Log in

Log in