Regular Expression

I’m trying to figure out a regular expression for filtering a characters name, but spec is nil. Is there anything that can be done?


do{
            
            "Enter your character's name. \n";
            me.name = inputLine();
            spec = '<period|plus|star|lparen|rparen|lsquare|rsquare|lbrace|rbrace|langle|rangle|
vbar|caret|period|squote|dquote|dot|percent|question|dollar|backslash|return|linefeed|tab|nul|null>';
            
            if(!me.name.match(spec) && me.name.match(spec) != 0)
            {
                nameAccept = true;
            }
            
        } while (!nameAccept);

You code is not complete, I don’t know where is spec declared and what is really reported as nil. But try if(!rexMatch(spec, me.name)) {…}

Or if you want to use match() method on string, then you should be aware the method does both simple string matching and regular expression matching depending on type of argument. If the pattern is plain string, then string matching is performed. You must insert pattern object for regular expression match. You can try something like me.name.match(R’your pattern here’). String prefixed with R makes a static RexPattern object for you.

Oh and maybe enumerating what should be in name instead of what shouldn’t could be shorter, try !rexMatch(’<^AlphaNum>’, me.name) or rexMatch(’^+$’, me.name). Character class includes not only ASCII, but all Unicode characters.

Thanks :slight_smile: this actually shortened my code by a lot. I’m going through and taking out all the long if statements, lol.

Wait, it worked once, and now giving errors. This is a slightly different section of code, but same issue. The error I’m getting is invalid comparison.

[code]commandArr = command.split(’ ');

        if(rexMatch('<Digit>', commandArr[2]) > 0)
        {
            
            if(rexMatch('<Alpha>', commandArr[2]) > 0)
            {
                
                "NAN";
            }
          
        // End of spend mass points command
        }[/code]

Never mind, I see what you’re saying about order of operation and cutting down. Sorry, no genius, but this works :slight_smile:

[code] if(!rexMatch(’’, commandArr[2]))
{

            "Good number";
            
        }[/code]

Invalid comparison is about your comparison with number zero “rexMatch(…) > 0”. Return value of rexMatch is a number when there is a match, but nil value otherwise and nil cannot be compared using arithmetic comparison operators. Just delete the “> 0” and you will be fine.