Java:用正则表达式从字符串中提取英文单词并以列表形式返回
要从字符串中获取英文单词并以列表形式返回,可以使用正则表达式。以下是一个Java方法示例: ```java import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String input = "这是一个包含English words的字符串"; List<String> words = getEnglishWords(input); System.out.println(words); } public static List<String> getEnglishWords(String input) { List<String> words = new ArrayList<>(); Pattern pattern = Pattern.compile("[a-zA-Z]+"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { words.add(matcher.group()); } return words; } } ``` 这个方法首先使用正则表达式`[a-zA-Z]+`匹配英文单词,然后将匹配到的单词添加到列表中并返回。 ######[AI写代码神器 | 255点数解答 | 2024-04-21 10:34:57]
- Java 实现复制粘贴功能:打造简易文本复制粘贴程序(GPT | 1497点数解答 | 2024-08-25 09:40:33)426
- C++ 求解:最少操作次数让字符串无子串 “friend” (字节豆包 | 153点数解答 | 2024-08-17 16:29:54)494
- C++ 求解:最少操作次数让字符串不再含 “friend” 子串!(讯飞星火 | 260点数解答 | 2024-08-17 16:30:49)478
- Python 巧解:最少操作次数让字符串告别“friend”子串(字节豆包 | 139点数解答 | 2024-08-18 13:04:14)418
- 3414 数字游戏:Python 求解 8 位 01 字符串中 1 的个数 (字节豆包 | 106点数解答 | 2024-10-27 14:23:27)444
- 巧用字符添加,计算字符串中“our”最大数量及开发注意要点(DeepSeek | 376点数解答 | 2025-10-30 19:20:06)93
- C 语言实现:处理字符串生成最多 “our” 串,附代码及关键开发注意点 (DeepSeek | 257点数解答 | 2025-10-30 19:20:36)127
- 解决数据库“dm.jdbc.driver.dmexception: 数据溢出”问题的全步骤指南(阿里通义 | 598点数解答 | 2024-10-15 15:48:38)328
- 达梦数据库数据溢出问题排查及多维度解决方法(阿里通义 | 403点数解答 | 2024-10-15 15:48:41)1207
- Python开发必备:字符串、列表、元组、字典常用的5个方法大揭秘!(阿里通义 | 325点数解答 | 2023-11-08 00:51:48)270
- 巧用正则表达式:精准匹配字符串中第二个重复字符(DeepSeek | 229点数解答 | 2025-06-12 15:27:32)126
- C 语言:输入正整数逆序建单链表并输出,附代码实现与测试用例!(GPT | 649点数解答 | 2024-06-17 15:04:31)325