Je cherche un peu d'aide sur certains Lua. J'ai besoin d'un peu de code pour correspondre à cette ligne exacte:
efs.test efs.test.gpg
Voici ce que j'ai jusqu'à présent, qui correspond à "efs.test":
if string.match(a.message, "efs.test", "efs") then print(a.message) else print ("Does not match") end
J'ai aussi essayé ceci, qui correspond à:
if string.match(a.message, "efs.test") then print(a.message) else print ("Does not match") end
Mais quand j'essaye d'ajouter le texte supplémentaire, mes erreurs de compilation avec "Nombre attendu, got string "lors de l'exécution de ce code:
if string.match(a.message, "%a+%a+%a+.%%a+%a+%a+%a+") then print(a.message) else print ("Does not match") end
Tout pointeur serait génial!
Merci.
3 Réponses :
string.match
Le troisième argument facultatif est l'index de la chaîne donnée pour commencer la recherche. Si vous recherchez exactement efs.test efs.test.gpg
dans cet ordre avec cet espacement donné, pourquoi ne pas simplement utiliser:
string.match(a.message, ".*efs%.test efs%.test%.gpg.*")
Si vous voulez faire correspondre la ligne entière contenant cette sous-chaîne:
string.match(a.message, "efs%.test efs%.test%.gpg")
if string.match(a.message, "%a+%a+%a+.%%a+%a+%a+%a+") then Firstly, this is a wrong use of quantifiers. From PiL 20.2: + 1 or more repetitions * 0 or more repetitions - also 0 or more repetitions ? optional (0 or 1 occurrence) In words, you try to match for unlimited %a+ after you already matched the full word with unlimited %a+To match efs.test efs.test.gpg - we have 2 filenames I suppose, in a strict sense file names may contain only %w - alphanumeric characters (A-Za-z0-9). This would correctly match efs.test:string.match(message, "%w+%.%w+")Going one step further, match efs.test as filename and the following filename:string.match(message, "%w+%.%w+ %w+%.%w+%.gpg")While this would match both filenames, you would need to check if matched filenames are the same. We can go one step further yet:local file, gpgfile = string.match(message, "(%w+%.%w+) (%1%.gpg)")This pattern will return any <filename> <filename>.gpg where the filenames are equal.With the use of capture-groups, we capture the filename: it will be returned as the first variable and further represented as %1. Then after the space char, we try to match for %1 (captured filename) followed by .gpg. Since it's also enclosed in brackets, it will become the second captured group and returned as the second variable. Done!PS: You may want to grab ".gpg" by case-insensitive [Gg][Pp][Gg] pattern.PPS: File names may contain spaces, dashes, UTF-8 characters etc. E.g. ext4 only forbids \0 and / characters.
Si vous essayez de faire correspondre cette ligne exacte, il est plus facile d'utiliser simplement:
if string.match(a.message, "%w+%.%w+") == "efs.test" then ... end
Bien sûr, cela ne trouverait aucune autre chaîne que celle-ci. Une autre interprétation que je vois pour votre question est que vous voulez savoir si elle contient efs.test dans la chaîne, ce que vous devriez pouvoir accomplir en faisant:
if "efs.test efs.test.gpg" = a.message then print(a.message) else print("string does not match!") end
Aussi, regardez dans regex, c'est fondamentalement le langage Lua utilisé pour faire correspondre les chaînes à quelques exceptions près.
Juste pour confirmer que vous cherchez à voir si
efs.test efs.test.gpg
existe dansa.message
? ou est-ce que vous chercheza.message
pour correspondre exactement àefs.test efs.test.gpg
?