[英]Regex java issue code not working
我寫的一些正則表達式代碼有問題。 該代碼基本上檢查了我的“ 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
}
}
}
}
當我運行代碼時,什么都沒有顯示,甚至沒有錯誤消息。 我重新檢查了我的代碼,沒有發現任何錯誤。
順便說一句,我正在使用android studio。
您以錯誤的順序傳遞了論點。 方法簽名將它們顛倒了
您的正則表達式不正確,因為您使用的aZ應該為“ z”(正常情況)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.