4

Using Java How to search about text between bracket in an expression like that "Request ECUReset for [*11 01]"

I want to extract "11 01" but I don't know exactly the length of the text between brackets.

may be :"Request ECUReset for [*11 01]" or : "Request ECUReset for [*11]" or : "Request ECUReset for [*11 01 10]" or any length

any help please ??

Heba Ahmed
  • 45
  • 4

2 Answers2

1

If the strings have a fixed format, String#substring() will suffice:

Online demo here.

public static void main(String[] args) {
    String input1 = "Request ECUReset for [*11 01]";
    String output1 = input1.substring(input1.indexOf("[*")+"[*".length(), input1.indexOf("]", input1.indexOf("[*")));
    System.out.println(input1 + " --> " + output1);

    String input2 = "Request ECUReset for [*11]";
    String output2 = input2.substring(input2.indexOf("[*")+"[*".length(), input2.indexOf("]", input2.indexOf("[*")));
    System.out.println(input2 + " --> " + output2);

    String input3 = "Request ECUReset for [*11 01 10]";
    String output3 = input3.substring(input3.indexOf("[*")+"[*".length(), input3.indexOf("]", input3.indexOf("[*")));
    System.out.println(input3 + " --> " + output3);
}

Output:

Request ECUReset for [*11 01] --> 11 01
Request ECUReset for [*11] --> 11
Request ECUReset for [*11 01 10] --> 11 01 10



Or, if the input is less stable, you can use a Regex (through the utility Pattern class) to match the number between the brackets:

Online demo here.

import java.util.regex.*;
public class PatternBracket {
    public static void main(String[] args) {
        String input1 = "Request ECUReset for [*11 01]";
        String output1 = getBracketValue(input1);
        System.out.println(input1 + " --> " + output1);

        String input2 = "Request ECUReset for [*11]";
        String output2 = getBracketValue(input2);
        System.out.println(input2 + " --> " + output2);

        String input3 = "Request ECUReset for [*11 01 10]";
        String output3 = getBracketValue(input3);
        System.out.println(input3 + " --> " + output3);
    }
    private static String getBracketValue(String input) {
        Matcher m = Pattern.compile("(?<=\\[\\*)[^\\]]*(?=\\])").matcher(input);
        if (m.find()) {
            return m.group();
        }
        return null;
    }
}

(same output as above)

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
0

Assuming that you don't have any nested brackets and you want to ignore * after [ you can use for example groups like in this example

String data="Request ECUReset for [*11 01]";
Matcher m=Pattern.compile("\\[\\*?(.*?)\\]").matcher(data);
while (m.find()){
    System.out.println(m.group(1));
}

Also if you know you have only one matching part you can use one-liner

System.out.println(data.replaceAll(".*\\[\\*?(.*?)\\].*", "$1"));
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • You shouldn't use `.*?`. This will cause back-tracking to happen and can destroy performance on large, non-matching strings, which is bad when using `Matcher.find()`. Use `[^\\]]*` instead, so you only match up to the first `]` without back-tracking. – Brian Jun 30 '13 at 21:04
  • @Brian after `.*?` I have only one literal `]` so this expression will do exactly as `[^\\]]*`. In situation where `]` will be found after `[` both expressions will act the same way (wont they?) – Pshemo Jun 30 '13 at 21:09
  • Some good info about the different quantifiers can be found [in this question](http://stackoverflow.com/questions/5319840/). [This answer](http://stackoverflow.com/a/5319996/646634) is particularly good, detailing the technical information about back-tracking and how it works with respect to the different quantifiers. – Brian Jun 30 '13 at 21:28
  • @Brian I get your idea, but I am not sure why do you think that with every `[*...]` regex engine starts matching from start, rather then from last match `[*..]`. – Pshemo Jun 30 '13 at 21:35
  • @Brian I am not saying you are wrong, just can't find any info about it that will confirm your example. – Pshemo Jun 30 '13 at 21:40
  • Give me a moment to find a source or tool that shows you how it gets processed. – Brian Jul 01 '13 at 02:07
  • I wasn't quite right, and for that, I apologize. However, it still takes more steps to match using `.*?`. I tested this using [Regex Buddy](http://www.regexbuddy.com/) (took me a bit to remember my license info and install it on this machine). While it doesn't increase `n^2`, it does increase `3n` plus some overhead. I'll move this convo into chat and post my results for you to see, and we can continue our discussion there if you wish. – Brian Jul 01 '13 at 03:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32655/discussion-between-brian-and-pshemo) – Brian Jul 01 '13 at 03:07