Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>'|'</code> character.</p> <pre><code>String attrs = "id|fullname|email|title"; String regex = "fullname\\|email"; return attrs.matches(regex); </code></pre> <p>The other problem is that your usecase really needs to be using <code>find</code> rather than <code>matches</code>, and the <code>String</code> API does not support <code>find</code>. This means you need to rewrite it to use an explicit <code>Pattern</code> and <code>Matcher</code>; e.g.</p> <pre><code>String attrs = "id|fullname|email|title"; Pattern regex = Pattern.compile("fullname\\|email"); return regex.matcher(attrs).find(); </code></pre> <p>But even this is not right:</p> <ol> <li>What you really need to do (I think) is match the supplied attributes independent of the order in which the user provides them. </li> <li>Naive regexes above will mismatch if the user enters something like "meaningfullname".</li> </ol> <p>Really, this is getting too complicated to do using regexes. Instead, you are better off with something like this:</p> <pre><code>List&lt;String&gt; 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"); } } </code></pre> <p>or if you just want to test if the attributes contain one or more of <code>fullname</code> and <code>email</code></p> <pre><code>String[] suppliedAttrs = supplied.split("\\|"); for (String s: suppliedAttrs) { if (s.equals("fullname") || s.equals("email")) { System.err.println("BINGO!"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload