java - 检索分号前的单词
问题描述
我有绳子,
String str ="public class Person {private String firstName;private String lastName;private int nId;}"
如果我想检索每个分号之前的单词,我会怎么做?所以输出将是,
firstName
lastName
nId
解决方案
您可以使用正则表达式来定位每个分号后面的单词:
public static void main(String[] args) {
String str ="public class Person {private String firstName;private String lastName;private int nId;}";
String pattern = "(\\w*);";
Matcher m = Pattern.compile(pattern).matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
}
分号后面的单词存储在m.group(1)
.
输出:
firstName
lastName
nId
推荐阅读
- php - PHP 表单验证并在输入字段旁边显示错误消息
- javascript - Android webview没有登录
- android - update() 没有在作为观察者的 Activity 中被调用
- android - 更改适配器和片段中的文本颜色
- python-3.x - OpenCV(imread)操作卡在弹性豆茎中
- vertical-alignment - 如何在 PlantUML 图上垂直显示块?
- android - 尽管设置了尺寸,但 ConstraintLayout 中的自定义视图最终尺寸为 0
- pandas - CoxPHFitter:输入必须至少有 3 个条目
- activemq - ActiveMQ 阻止消费者处理特定消息
- reactjs - 更新firebase数据库后“警告:无法在未安装的组件上调用setState(或forceUpdate)”