简体   繁体   中英

Using regular expression to find a set number of + JAVA

I have a program where I want to filter Strings with a set number of "+"'s at the beginning. For example:

+++Adam is working very well. 
++Adam is working well. 
+Adam is doing OK. 

How do I only pick up each particular case (ie only one plus sign, only two plus signs, only three plus signs)? I usually get a return of anything beginning with a +.

I have the following regex patterns compiled, but I either get only one return (usually the two ++) or all of them:

public static String regexpluschar = "^\\Q+\\E{1}[\\w <]";
public static String regexpluspluschar = "^\\Q+\\E{2}[\\w <]";
public static String regexpluspluspluschar = "^\\Q+\\E{3}[\\w <]";
Pattern plusplusplus = Pattern.compile(regexpluspluspluschar);
Pattern plusplus = Pattern.compile(regexpluspluschar);
Pattern plus = Pattern.compile(regexpluschar);

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).

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);   
}

For the very curious, the above if's are embedded in a for loop that cycles around some JTextFields. Full code at: http://pastebin.ca/2199327

Why not simpler :

public static String regexpluschar = "^\\+[\\w <]";
public static String regexpluspluschar = "^\\+{2}[\\w <]";
public static String regexpluspluspluschar = "^\\+{3}[\\w <]";

or even

public static String regexpluschar = "^\\+[^\\+]";
public static String regexpluspluschar = "^\\+{2}[^\\+]";
public static String regexpluspluspluschar = "^\\+{3}[^\\+]";

Edit : It's working on my test program, but I had to removed your specific code :

String toChange = getItem(i);
setItemFiltered(i, toChange);

proof : my output is :

Filtering 3 +s.
+++Adam is working very well.  is in the 3 + filter.
Filtering 2 +s.
++Adam is working well.  is in the 2 + filter.
Filtering 1 +.
+Adam is doing OK.  is in the 1 + filter.

Your filter is working, but you specific code may not... (maybe have a look at setItemFiltered ?)

I was thinking something like this would be easier:

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("^(\\+{1,3}).*");

    Matcher matcher = pattern.matcher(<your text>);
    if (matcher.matches()) {
        String pluses = matcher.group(1);
        switch (pluses.length()) {

        }
    }

}

And if you want to be sure that ++++This is insane does not match then change the pattern to

Pattern pattern = Pattern.compile("^(\\+{1,3})[^+].*");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM