【发布时间】:2017-10-09 21:38:26
【问题描述】:
我正在尝试从领事输出中提取一组字符串。 我想要做的是删除所有以
开头的字符串实例/usr/lib/ocf/resource.d/
输入字符串
-rwxr-xr-x. 1 root root 971 Sep 22 13:15 /usr/lib/ocf/resource.d/cloud_init_ocf.sh/n-rwxr-xr-x. 1 root root 662 Aug 28 11:25 /usr/lib/ocf/resource.d/credentialmanagercliRestartVM.sh/n-rwxr-xr-x. 1 root root 843 Sep 28 11:13 /usr/lib/ocf/resource.d/jboss_healthcheck.sh
在上面的示例字符串中,将是
的字符串/usr/lib/ocf/resource.d/cloud_init_ocf.sh
/usr/lib/ocf/resource.d/credentialmanagercliRestartVM.sh
/usr/lib/ocf/resource.d/jboss_healthcheck.sh
我的尝试
我试图匹配以开头的字符串
\\b/usr/lib/ocf/resource.d/.*\\b
I got from here
代码
regexChecker("\\b/usr/lib/ocf/resource.d/.*\\b", output);
private ArrayList<String> regexChecker(String regEx, String str2Check) {
final ArrayList<String> result = new ArrayList<>();
Pattern checkRegex = Pattern.compile(regEx);
Matcher regexMatcher = checkRegex.matcher(str2Check);
String regexMatch;
while (regexMatcher.find()) {
if (regexMatcher.group().length() != 0) {
regexMatch = regexMatcher.group();
result.add(regexMatch);
}
}
return result;
}
我认为问题在于插入在每行末尾的/n 字符。
【问题讨论】:
-
因此,实际上,您想要“替换”某些字符串。对吗?
-
试试
/usr/lib/ocf/resource.d/.*ref -
你的输入字符串真的没有换行符吗?
-
@TheLostMind 我的想法是,我想从子字符串中提取一个字符串。
-
试试这个正则表达式
(\/usr\/lib\/ocf\/resource\.d\/[a-zA-Z_]*(\.sh[\s|]?)?)