-3

I am trying to parse data from the [] in a String in java

String text = "some text [Karan] some text";

I want to the computer to read Karan present inside the brackets

I have tried String.split() method but it packs it into an array which I don't want.

Is there any way to do this. Thank you

Karan Gandhi
  • 45
  • 13
  • 1
    Someone will downvote you because you didn't try anything to ask for help. You asked someone to do it for you. SO rules say to try something and then ask for help. By the way, a little search will get you the answer. – Amir Raminfar Jul 03 '19 at 16:23
  • those are called brackets, not parens – RobOhRob Jul 03 '19 at 16:42

3 Answers3

2

You can use regex like this:

String text = "some text [Karan] some [test2] text [test3] [test4] 22[test5]";
Pattern pattern = Pattern.compile("(?<=\\[).*?(?=\\])");
//or use this regex,it works well too
//Pattern pattern = Pattern.compile("(?<=\\[)[^\\[\\]]*(?=\\])");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
    System.out.println(matcher.group());
}

and the result is Karan,test2,test3,test4,test5. Regex is useful for processing text.This improved version of regular is using "Positive and Negative Lookbehind".Thanks for Matthieu's suggestions.

Mr.DW
  • 106
  • 6
  • The only valid answer. Though you might want to check *look-aheads* to exclude the brackets themselves from the groups. – Matthieu Jul 04 '19 at 02:29
  • Yes,because the bracket is only the keyword to distinguish what in it and out. – Mr.DW Jul 06 '19 at 10:23
  • I mean use "positive and negative look behind". You can use `(?<=\[).*(?=\])` to capture *only* what is between brackets, without capturing the brackets themselves. See https://regex101.com/ to test, and the awesome https://www.regular-expressions.info/ for all about regex. – Matthieu Jul 06 '19 at 10:32
  • Even better, to separate all groups: `(?<=\[)[^\[\]]*(?=\])` – Matthieu Jul 06 '19 at 10:36
  • 1
    Your suggestion is great.And I have tried two solutions.For the first regex (?<=\[).*(?=\]) it has a little short,it will match the text like "Karan] some [word2".So compensate for it should be (?<=\[).*?(?=\]),using the not greedy pattern.And the second regex is another good way to match the text.I will edit my answer later with your suggestions. – Mr.DW Jul 06 '19 at 11:03
1

You might want to use the String.substring method in combination with String.indexOf.

Here is a short description of how you would extract text between brackets [] using these two methods:

  1. Get the index of the first bracket [ character using String.indexOf method. (let's call this value start)
  2. Get the index of the second bracket ] character using String.indexOf method (let's call this value end)
  3. Call .substring on the string you are extracting information from with

    text.substring(start+1, end)
    

Note how the first index start+1 is inclusive and end is exclusive.

Samantha
  • 275
  • 1
  • 12
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
-1

Looks like this then:

String text = "some text [Karen] some text";
text = text.substring(text.indexOf("[") + 1, text.indexOf("]"));
System.out.println(text);