0

This is the input string xxx.xx.xx.xx - - [30/Oct/2020:22:39:54 +0000] -- I want to find date from this particular String i.e. How I suppose to get this Value I want to find pattern "- - [" and want to escape first backslash "/".

I tried below code

Matcher matcher = Pattern.compile("^(.*)- - "["").matcher(data);
 if(matcher.find()){
    String date = matcher.group();
    System.out.println(date);
  }

But , it's not taking opening square brace "["

Sakura
  • 7
  • 3

2 Answers2

0

You can use regex to find and replace the first character as below:

String data = "xxx-xxx-xxx - - [30/Oct/2020:22:39:54 +0000] --";
        Matcher matcher = Pattern.compile("(?=- - \\[)([^/]*)/")
                                 .matcher(data);
        if (matcher.find()) {
            System.out.println(matcher.replaceAll("$1\\\\/"));
}

This will output:

xxx-xxx-xxx - - [30\/Oct/2020:22:39:54 +0000] --

Regex Pattern: (?=- - \[)([^/]*)/

Explanation: Use Postive Look behind to match - -[.

Prasanna
  • 2,390
  • 10
  • 11
0

I tried with below code it worked

Matcher matcher = Pattern.compile("^(.*)- - (.*)$").matcher(data);
                    if(matcher.find()){
                        String date = matcher.group(2).substring(1,3);
                        
                        System.out.println(date);
                    }

However I looking any other solution using regular expression

Sakura
  • 7
  • 3