Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is: you accidentally swapped the use of the regexp pattern and the expression to check</p> <pre><code>String exp = "ABCD_123_abc"; String regExp = "([a-zA-Z]+)_([0-9]+)_([a-z]+)"; </code></pre> <p>Should be used </p> <pre><code>Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(exp); </code></pre> <p>The <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#compile%28java.lang.String%29">Pattern.compile(String regex)</a> function accepts the regular expression.</p> <p><strong>EDIT</strong></p> <p>I apologize, my first solution was truly something that must <strong>never ever, <em>never</em> ever</strong> be done: the names of the variables were contradictory to the meaning of their values... That means pain and tears, and getting hit by angry colleagues while being shouted at. And there is no valid defense to this <strong>crime</strong>...</p> <p><strong>EDIT2</strong> You can get the individual matched groups by the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html#group%28int%29">Matcher.group(int)</a> function:</p> <pre><code>String matchedStringpart matcher.group(2); </code></pre> <p>Notice: I used <code>2</code> as the argument:</p> <ul> <li><code>0</code> means the input sequence matched</li> <li><code>1</code> means the first group (<code>ABC</code> in this case)</li> <li>... and so on</li> </ul> <p>If you only need the <code>123</code> part, I'd rewrite the regex for clarity:</p> <pre><code>regExp = "[a-zA-Z]+_([0-9]+)_[a-z]+"; </code></pre> <p>However, in that case, the <code>group()</code> has to be called with <code>1</code>, as now the first (and only) matched group is the first one:</p> <pre><code>String matchedStringpart matcher.group(1); </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. 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