简体   繁体   English

正则表达式Java问题代码不起作用

[英]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. 该代码基本上检查了我的“ longString”,其中包含要检查的字符串,并输出与正则表达式匹配的字符串中的单词。

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. 顺便说一句,我正在使用android studio。

You are passing in your arguments in the wrong order. 您以错误的顺序传递了论点。 The method signature has them reversed 方法签名将它们颠倒了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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