4

I am trying to make the following match, but it is not returning true as I would expect:

String attrs = "id|fullname|email|title";
String regex = "fullname|email";
return attrs.matches(regex);

Instead it returns false.

Why is this? I expect the | pipe in attrs to be interpreted literally as an ASCII character, and the | pipe in regex to be interpreted according to regular expressions (i.e. OR).

The reason I ask is because I am writing an application where I let the user set the attributes in the format attr1|attr2|attr3|..., and I want to verify his/her input by matching it against the possible attribute values: attr1|attr2|attr3|...|attr[n].

Help appreciated,
ktm


Got it working by doing

String regex = "id.*|fullname.*|email.*|title.*";
String attrs = "fullname|email";
return attrs.matches(regex);
0

3 Answers 3

7

java String::matches() matches only whole strings. You need something like

try: regex =".*(fullname|email).*;

Or use the Pattern class

A better way of doing what you want is String[] rattrs = attrs.split("\\|") and then check each string.

7
  • Right! I forgot to write .* It's been so long since I've done regex. I got it working by making the change reflected in my original question.
    – ktm5124
    Jan 23, 2011 at 0:19
  • Why do you think the other way is better? It involves more code! (Less computation?)
    – ktm5124
    Jan 23, 2011 at 0:25
  • I suggest you try running the suggested code and inspecting its results. It is not doing what you think it is doing.
    – tchrist
    Jan 23, 2011 at 0:28
  • I have a finite set of attributes (actually the four listed are the complete set). I believe the only case in which it would not work is if there is a leading sequence of characters that does not match an attribute. But in that case the program would not be properly configured and I would not want it to run. Also: the code passed all of my tests.
    – ktm5124
    Jan 23, 2011 at 0:40
  • 2
    Actually, split("\|") won't even compile. To split on the regex \| you have to write split("\\|").
    – Alan Moore
    Jan 23, 2011 at 1:56
7

The problem is that the pipe character is a meta-character in regexes. It therefore needs to be escaped if you want to match a literal '|' character.

String attrs = "id|fullname|email|title";
String regex = "fullname\\|email";
return attrs.matches(regex);

The other problem is that your usecase really needs to be using find rather than matches, and the String API does not support find. This means you need to rewrite it to use an explicit Pattern and Matcher; e.g.

String attrs = "id|fullname|email|title";
Pattern regex = Pattern.compile("fullname\\|email");
return regex.matcher(attrs).find();

But even this is not right:

  1. What you really need to do (I think) is match the supplied attributes independent of the order in which the user provides them.
  2. Naive regexes above will mismatch if the user enters something like "meaningfullname".

Really, this is getting too complicated to do using regexes. Instead, you are better off with something like this:

List<String> attrs = Arrays.asList(
        new String[] {"id", "fullname", "email", "title"});
String[] suppliedAttrs = supplied.split("\\|");
for (String s: suppliedAttrs) {
    if (!attrs.contains(s)) {
        throw new IllegalArgumentException("'" + s + "' is not valid");
    }
}

or if you just want to test if the attributes contain one or more of fullname and email

String[] suppliedAttrs = supplied.split("\\|");
for (String s: suppliedAttrs) {
    if (s.equals("fullname") || s.equals("email")) {
        System.err.println("BINGO!");
    }
}
2
  • Thanks! I actually don't want to match a literal '|' character. I'm just using it as a syntactical means of separating attributes.
    – ktm5124
    Jan 23, 2011 at 0:18
  • @ktm5124 - I figured that out. See my updated answer.
    – Stephen C
    Jan 23, 2011 at 0:22
2

You're using matches, not find, so it must correspond with the whole string.

Not the answer you're looking for? Browse other questions tagged or ask your own question.