Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Solution (partial)</h2> <p><strong>Please also check the next section. Don't just read the solution here.</strong></p> <p>Just modify your code a bit:</p> <pre class="lang-java prettyprint-override"><code>String test = "xyz_stringIAmLookingFor_zxy"; // Make the capturing group capture the text in between (\w*) // A capturing group is enclosed in (pattern), denoting the part of the // pattern whose text you want to get separately from the main match. // Note that there is also non-capturing group (?:pattern), whose text // you don't need to capture. Pattern p = Pattern.compile("_(\\w*)_"); Matcher m = p.matcher(test); while (m.find()) { // find next match // The text is in the capturing group numbered 1 // The numbering is by counting the number of opening // parentheses that makes up a capturing group, until // the group that you are interested in. String match = m.group(1); System.out.println(match); } </code></pre> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28%29" rel="nofollow"><code>Matcher.group()</code></a>, without any argument will return the text matched by the whole regex pattern. <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28int%29" rel="nofollow"><code>Matcher.group(int group)</code></a> will return the text matched by capturing group with the specified group number.</p> <p>If you are using Java 7, you can make use of <em>named capturing group</em>, which makes the code slightly more readable. The string matched by the capturing group can be accessed with <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28java.lang.String%29" rel="nofollow"><code>Matcher.group(String name)</code></a>.</p> <pre><code>String test = "xyz_stringIAmLookingFor_zxy"; // (?&lt;name&gt;pattern) is similar to (pattern), just that you attach // a name to it // specialText is not a really good name, please use a more meaningful // name in your actual code Pattern p = Pattern.compile("_(?&lt;specialText&gt;\\w*)_"); Matcher m = p.matcher(test); while (m.find()) { // find next match // Access the text captured by the named capturing group // using Matcher.group(String name) String match = m.group("specialText"); System.out.println(match); } </code></pre> <h2>Problem in pattern</h2> <p>Note that <code>\w</code> also matches <code>_</code>. The pattern you have is ambiguous, and I don't know what your expected output is for the cases where there are more than 2 <code>_</code> in the string. And do you want to allow underscore <code>_</code> to be part of the output?</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. 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