Link to home
Start Free TrialLog in
Avatar of ptreves
ptreves

asked on

MutableTable Regular Expression

Hello,

I have a global constant that allows the validation of fields from a MutableTable.

    public static final String POINT_GROUP_REGEX = "[^\\r\\n\\t]*";

    public static final String POINT_GROUP_REGEX = "[^|;&\\r\\n\\t]*";

to take into account the special characters. |&;
Is that all that is required to fix this bug ?

Paolo
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Yes. If you wanted to match a | character without listing other possible characters in [] brackets, you'd need to escape it like \\| but it's ok while it's contained in [] brackets.
That should work, yes.
I'm a little confused though what this has to do with Mutable Tables...
Note also that your pattern is matching ^ as a character, not as a start-of-string placeholder; I'm not sure whether that's what you intended.
And actually, I wonder if the expression is a bit meaningless in the first place. The * after the characters in [] allows zero characters to be matched, which probably isn't what you want. Maybe you want:
    public static final String POINT_GROUP_REGEX = "[^|;&\\r\\n\\t]+";
Changing the * to + means the pattern requires at least one of the listed characters.
Avatar of ptreves
ptreves

ASKER

Changing the quantifier from * to +, as follows:

    public static final String POINT_GROUP_REGEX = "[^|;&\\r\\n\\t]+";

What I realy want is to prevent users from entering any of the three special characters  |;& in a mutable table used for "Group Names". As soon as a user enters any of those three special characters or any of the control characters, he should be prompted to change/correct his entry.

Here is the java routine that processes the Regular Expression:

        ColumnTableModel pointGroupsModel;
        Column groupID = getColumn(messages.getString("GroupIDTxt"), false, SystemwideConstants.POINT_GROUP_ID_ATTR, Integer.class, null,
                false, null);

        Column pointGroupName = getStringColumn(messages.getString("GroupNameTxt"), SystemwideConstants.POINT_GROUP_NAME_ATTR,
                new RegexEditor(SystemwideConstants.POINT_GROUP_REGEX, new InvalidStringHandler(messages.getString("GroupNameTxt"))));

        Column[] columns = new Column[]{groupID, pointGroupName};
        pointGroupsModel = new ColumnTableModel(columns, stemwideConstants.MAX_NUMBER_OF_POINT_GROUPS);

The problem is that after compiling and testing the code, the user can still enter those special characters.
Is the regular expression at fault ?

Paolo
Q: Is the regular expression at fault ?
A: Perhaps...

   Where, and how is the RegExp used?  Are we talking about a code in your setValueAt() method?
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial