Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing regular expression to find a set number of + JAVA
    primarykey
    data
    text
    <p>I have a program where I want to filter Strings with a set number of "+"'s at the beginning. For example:</p> <pre><code>+++Adam is working very well. ++Adam is working well. +Adam is doing OK. </code></pre> <p>How do I only pick up each particular case (i.e. only one plus sign, only two plus signs, only three plus signs)? I usually get a return of anything beginning with a +. </p> <p>I have the following regex patterns compiled, but I either get only one return (usually the two ++) or all of them: </p> <pre><code>public static String regexpluschar = "^\\Q+\\E{1}[\\w &lt;]"; public static String regexpluspluschar = "^\\Q+\\E{2}[\\w &lt;]"; public static String regexpluspluspluschar = "^\\Q+\\E{3}[\\w &lt;]"; Pattern plusplusplus = Pattern.compile(regexpluspluspluschar); Pattern plusplus = Pattern.compile(regexpluspluschar); Pattern plus = Pattern.compile(regexpluschar); </code></pre> <p>I then try to find using a Matcher class - I've used .find() and .matches() but don't get the result I'm after (java+regex newbie alert here). </p> <pre><code>Matcher matcherplusplusplus = plusplusplus.matcher(check); Matcher matcherplusplus = plusplus.matcher(check); Matcher matcherplus = plus.matcher(check); //OK we have 3+'s if ((matcherplusplusplus.find())==true){ System.out.println("Filtering 3 +s."); System.out.println("filter is " + filter + " in the 3 + filter."); String toChange = getItem(i); setItemFiltered(i, toChange); } //OK - we have 2 +'s if ((matcherplusplus.find())==true){ System.out.println("Filtering 2 +s."); System.out.println("filter is " + filter + " in the 2 + filter."); String toChange = getItem(i); setItemFiltered(i, toChange); } //OK - we have 1 +'s if ((matcherplus.find())==true){ System.out.println("Filtering 1 +."); System.out.println("filter is " + filter + " in the 1 + filter."); String toChange = getItem(i); setItemFiltered(i, toChange); } </code></pre> <p>For the very curious, the above if's are embedded in a for loop that cycles around some JTextFields. Full code at: <a href="http://pastebin.ca/2199327" rel="nofollow">http://pastebin.ca/2199327</a></p>
    singulars
    1. This table or related slice is empty.
    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.
 

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