0

I am trying to extract a set of strings from a consul output. What I want to do is remove all instances of the strings which start with

/usr/lib/ocf/resource.d/

Input String

-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

In the above example string that would be the strings of

/usr/lib/ocf/resource.d/cloud_init_ocf.sh
/usr/lib/ocf/resource.d/credentialmanagercliRestartVM.sh
/usr/lib/ocf/resource.d/jboss_healthcheck.sh

What I have tried

I have tried to match the Strings which start with \\b/usr/lib/ocf/resource.d/.*\\b
I got from here

Code

 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;
    }

I think the issue is the /n character which is inserted at the end of each line.

12
  • So, effectively, you want to "replace" certain Strings. Right? Commented Oct 9, 2017 at 11:07
  • try /usr/lib/ocf/resource.d/.* ref Commented Oct 9, 2017 at 11:09
  • is your input string really without newlines? Commented Oct 9, 2017 at 11:25
  • @TheLostMind the way i think about it is, I want to extract a string from with in a sub string. Commented Oct 9, 2017 at 11:39
  • 1
    try this regex (\/usr\/lib\/ocf\/resource\.d\/[a-zA-Z_]*(\.sh[\s|]?)?) Commented Oct 9, 2017 at 12:40

1 Answer 1

2

try this way

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{
    public static void main(String[] args) {

        String regex = "(\\/usr\\/lib\\/ocf\\/resource\\.d\\/[a-zA-Z_]*(\\.sh[\\s|]?)?)";
        String string = "-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";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        int i =1;
        while (matcher.find()) {
                System.out.println("Group " + i++ + ": " + matcher.group(0));
        }
    }
}

and output is

Group 1: /usr/lib/ocf/resource.d/cloud_init_ocf.sh
Group 2: /usr/lib/ocf/resource.d/credentialmanagercliRestartVM.sh
Group 3: /usr/lib/ocf/resource.d/jboss_healthcheck.sh
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.