我有 Java 問題。有一個 Web 應用程序,它具有在后端運行 Javascript 和 Java 的搜索功能。我們只能配置 Java 源代碼,不能配置 Javascript。問題是有一個ArrayList<UserDTO>. 每個UserDTO包含Id,FirstName,LastName,和email。當我*在搜索引擎中輸入時,所有的結果都List出現了。問題出在電子郵件上。如果我搜索一個名字或姓氏,則沒有問題。當我搜索一封電子郵件時,沒有任何效果。它只有在我搜索這樣的內容時才有效:fe 電子郵件是
[email protected],如果我輸入,* gchat@mail *我就會找到它。如果我輸入.,在那之后,什么都不起作用。另外,如果我不輸入*,則沒有任何效果。fe 如果我只輸入這樣的電子郵件:gchat@mail沒有任何效果。這個的源代碼是:public ResponseEntity<List<UserDTO>> search(@PathVariable("query") String query) { List<UserDTO> results = new ArrayList<>(); if (query != null && !query.trim().isEmpty()) { for (UserDTO user : USERS) { String regExp = "^" + query.trim().replace("*", ".*") + "$"; Pattern pattern = Pattern.compile(regExp, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); Matcher firstnameMatcher = pattern.matcher(user.getFirstName()); Matcher lastnameMatcher = pattern.matcher(user.getLastName()); Matcher emailMatcher = pattern.matcher(user.getEmail()); if (firstnameMatcher.matches() || lastnameMatcher.matches() || emailMatcher.matches()) { results.add(user); } } }清單是這樣的:private static final List<UserDTO> USERS = new ArrayList<>(); static { USERS.add(new UserDTO("jpap", "John", "Papadopoulos", "
[email protected]", true, "EL", new HashSet<>())); USERS.add(new UserDTO("kpav", "Konstantinos", "Pavlopoulos", "
[email protected]", true, "EL", new HashSet<>())); USERS.add(new UserDTO("echar", "Eleni", "Charalampous", "
[email protected]", true, "EL", new HashSet<>())); USERS.add(new UserDTO("gchat", "Georgia", "Chatzipavlou", "
[email protected]", true, "EL", new HashSet<>())); USERS.add(new UserDTO("avel", "Apostolos", "Velis", "
[email protected]", true, "EL", new HashSet<>())); USERS.add(new UserDTO("sliol", "Sofia", "Lioliou", "
[email protected]", true, "EL", new HashSet<>())); }請問有人會幫助我嗎?我嘗試了不同的類型,但沒有任何工作正常。