Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see several minor problems in your code:</p> <ul> <li>If <code>varContainingNameOfSpecificFolder</code> contains characters that have a meaning in regular expressions (<code>.</code> being the most likely candidate), then you <strong>must</strong> use <code>Pattern.quote()</code> to quote that part.</li> <li>your use of <code>String.format()</code> is unnecessary, you can use simply <code>String</code> concatenation using <code>+</code> here, which is easier to read.</li> <li>you don't need the assignment to <code>myValue</code>, simply <code>return</code> the result of <code>substring</code></li> <li>the <code>substring</code> call is <em>unnecessary</em> here! Simply replacing that expression with <a href="http://download.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28int%29" rel="nofollow"><code>matcher.group(1)</code></a> has the same effect!</li> <li>using <a href="http://download.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29" rel="nofollow"><code>String.replaceAll()</code></a> might be a valid shortcut, but you'd have to modify your regex slightly.</li> </ul> <p>So my version would be:</p> <pre><code>public static String getValueThatIneed(String path) { String regex = ".*\\\\" + Pattern.quote(varContainingNameOfSpecificFolder) + "\\\\([0-9]+)\\\\.*"; String result = path.replaceAll(regex, "$1"); if (result.equals(path)) { throw new IllegalArgumentException("&lt;" + path + "&gt; does not contain the thin i need!"); } return result; } </code></pre> <p>If you don't care about error checking (as your code doesn't do it either) you can simply <em>return</em> the result of the <code>replaceAll()</code> method and have a two-line method:</p> <pre><code>public static String getValueThatIneed(String path) { String regex = ".*\\\\" + Pattern.quote(varContainingNameOfSpecificFolder) + "\\\\([0-9]+)\\\\.*"; return path.replaceAll(regex, "$1"); } </code></pre> <p>However, I <strong>strongly</strong> encourage you to keep it, as otherwise you can easily miss nasty bugs in your code.</p>
    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. 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