Hoopy frood
Joined: Feb 2004
Posts: 2,013 |
I should warn that using regex has consequences that should be taken into consideration. - Firstly, as you may or may not know, there are many characters that have a special meaning in a regex pattern. Some of those can be found in a nickname, so they should be escaped.
For instance in a regex | stands for alternation (or), [ ] stands for a character class, a . matches any character etc etc.
Someone with the nick [on] would trigger on any nickname that has either an o or n in their nick in the file. The nickname jake|afk would trigger on jake or afk. Definitely not what we were looking for.
You can escape special characters in a regex by either prefixing them with a backslash \ or by putting the pattern that needs escaping between \Q \E. Be careful! It is possible that $knick evaluates to something with \E in it, which would mess up our initial \Q \E, so that should also be escaped, fex by using a $replacecs on it. We'd end with something like:
$read(protect.txt,nr,$+(/^\Q,$replacecs($knick,\E,\Q\\E\E),\E$/i))
(for those of you wondering, we cannot use the x modifier here since the spaces are not ignored when escaped between \Q \E, or in a character class etc.) - Secondly, if the slashes / / do not enclose the regex, you may encounter weird results in some cases, for example if your pattern starts with an m. Try this //echo -a $regex(a,moo). You'd expect this to return 0, but in fact it gives 1. A nickname may very well start with an m, which is bound to give unrequired results.
- Thirdly, a pattern in a regex is always case sensitive unless specified otherwise by using the i modifier (which I added in the example). If case sensitivity isn't disabled, then "FO" will not match "fo".
- Finally, as you know, a regex matches as if there were wildcards around it when using iswm (*pattern*) unless it has been specified differently by adding boundaries around the pattern. In this case, a nickname is unique so should only match 1 nickname, not any others. The nick "one" may not match in the nick "Theone". Therefore I've added the anchors ^ and $ denoting that the matched string should start with the first letter of $knick, and end with the last letter of $knick, or in other words: a unique match.
- Taking all of this into consideration, the easiest most straightforward method is to use the w flag with $knick. No wildcards should be added so that $read matches a unique nickname, not a false positive.
$read(protect.txt,nw,$knick)
Even though I replied to you, this is more of a general reply to anyone using regex without being fully aware of the consequences. Greets
|