简体   繁体   中英

How do I remove certain special characters from a string in Lua?

Within Lua, how would I remove specific special characters from a string?
For example, a name input would be:

  • L@)iAm PAGE changed to Liam Page
  • José Luis changed to Jose Luis
  • JACK O'NIEL changed to Jack O'Niel

I currently have

firstName = ipFirstName:gsub('[%p%c%s]', '')
lastName = ipLastName:gsub('[%p%c%s]', '')

but it is too broad.

Below is a simple function for sanitizing names, to a certain degree:

local function sanitizeName (name)
    local accented = {
        ['ß'] = 'ss'
      , ['à'] = 'a', ['á'] = 'a', ['â'] = 'a', ['ã'] = 'a', ['å'] = 'a'
      , ['ä'] = 'ae', ['æ'] = 'ae'
      , ['ç'] = 'c'
      , ['è'] = 'e', ['é'] = 'e', ['ê'] = 'e', ['ë'] = 'e'
      , ['ì'] = 'i', ['í'] = 'i', ['î'] = 'i', ['ï'] = 'i'
      , ['ð'] = 'dh'
      , ['ñ'] = 'n'
      , ['ò'] = 'o', ['ó'] = 'o', ['ô'] = 'o', ['õ'] = 'o', ['ø'] = 'o'
      , ['ö'] = 'oe'
      , ['ù'] = 'u', ['ú'] = 'u', ['û'] = 'u'
      , ['ü'] = 'ue'
      , ['ý'] = 'y', ['ÿ'] = 'y'
      , ['þ'] = 'th'
    }
    local sanitized = name
        :lower()                        -- Bring everything to lower case.
        :gsub ('%s+', ' ')              -- Normalise whitespaces.
    -- Replace some non-ASCII characters:
    for fancy, plain in pairs (accented) do
        sanitized = sanitized:gsub (fancy, plain)
    end
    return sanitized
        :gsub ("[^%a ']", '')           -- Remove everyting but ASCII, spaces and apostrophes.
        :gsub ('^%a', string.upper)     -- Capitalise the first letter of the first name.
        :gsub ("[ ']%a", string.upper)  -- Capitalise the first letter of other names.
end

for _, name in ipairs {'L@)iAm PAGE', 'José Luis', "JACK O'NIEL"} do
    print (name, sanitizeName (name))
end

However, to deal with Unicode character properly, study this page . Also note that most of assumptions about personal names are false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM