简体   繁体   中英

Regex java issue code not working

I have an issue with a little regex code I wrote. The code basically checks my "longString" which contains the string that's being checked and outputs the words in the string that matches the regex.

import java.util.regex.*;

public class regexPractice 
{

    public static void main(String[] args) {
        String  longString = " Derek Banas CA ";

        regexChecker(longString, "\\s[A-Za-z]{2,20}\\s");
    }

    public static void regexChecker(String theregex, String stringCheck) {
        //theregex is the regex your searching for

        Pattern Checkregex = Pattern.compile(theregex);

        Matcher regexMatcher = Checkregex.matcher(stringCheck);

        while (regexMatcher.find()) { //kicks out all the matches for you
            if (regexMatcher.group().length() != 0) {
                System.out.println(regexMatcher.group().trim());
                //trim gets rid of all the white space
            }
        }
    }
}

When I run the code nothing shows up not even an error message. I rechecked my code and didn't find any errors.

By the way, I'm using android studio.

You are passing in your arguments in the wrong order. The method signature has them reversed

您的正则表达式不正确,因为您使用的aZ应该为“ z”(正常情况)。

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