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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
DevRight
  • 345
  • 1
  • 6
  • 25
  • So, effectively, you want to "replace" certain Strings. Right? – TheLostMind Oct 09 '17 at 11:07
  • try `/usr/lib/ocf/resource.d/.*` ref – davidchoo12 Oct 09 '17 at 11:09
  • is your input string really without newlines? – guido Oct 09 '17 at 11:25
  • @TheLostMind the way i think about it is, I want to extract a string from with in a sub string. – DevRight Oct 09 '17 at 11:39
  • @GUIDO I am not sure what you mean by with out newlines. The input string contains newlines at the end of each line -rwxr-xr-x. 1 root root 971 Sep 22 13:15 /usr/lib/ocf/resource.d/cloud_init_ocf.sh/n – DevRight Oct 09 '17 at 11:40
  • The input string in your question contains no linefeed, instead it contains the two characters `/n` where you would expect a linefeed. While `\n` is a way to represent the Line Feed character, `/n` are just two characters without any special meaning. – Aaron Oct 09 '17 at 12:29
  • Anyway you're using word-boundaries wrong by using them around non-word characters : using a `\b` before the `/` in your pattern means it must be preceded by a word character (since a word boundary is the boundary between a word and something else, and that `/` isn't a word character, so it must be the 'something else' and must be preceded by a word) – Aaron Oct 09 '17 at 12:34
  • 1
    try this regex `(\/usr\/lib\/ocf\/resource\.d\/[a-zA-Z_]*(\.sh[\s|]?)?)` – Rahmat Waisi Oct 09 '17 at 12:40
  • @RahmatWaisi That worked great. Some of the strings end in .bsh also. – DevRight Oct 09 '17 at 12:47
  • @RahmatWaisi so I added (\.sh|\.bsh[\s|]?)?) and it worked fine. thanks again. – DevRight Oct 09 '17 at 12:51
  • @RahmatWaisi create an answer and I will accept it. – DevRight Oct 09 '17 at 13:00
  • @SeanMcGrath you're welcome, ok, sorry i was busy. – Rahmat Waisi Oct 09 '17 at 13:33

1 Answers1

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
Rahmat Waisi
  • 1,293
  • 1
  • 15
  • 36