0

Let say I have a list of strings like:

List myList = new ArrayList();
myList.add("person [something]");
myList.add("person 'something'");

so in my strings sometimes I have [ and sometimes '

and now I would like to get substrings but I don't know how to do this if I would like to cut my string in one of the two chars. Can anyone tell me whether that option is possible? I tried something like:

List finalOne = new ArrayList();
for(String ss: myList){
finalOne.add(ss.substring("[" || "'" ))

}

I expect to get all substrings starting from char "[" or starting from "'" in one run

EdXX
  • 872
  • 1
  • 14
  • 32

1 Answers1

3

You can use (?<=\[)(.*?)(?=\]) and (?<=\')(.*?)(?=\') regular expressions to find the string between square brackets and single quotes.

You can take a look at this, for searching pattern in java, and this, for the regular expression.

uneq95
  • 2,158
  • 2
  • 17
  • 28
  • I need further help :( I did something like that in java String regex = "(?<=\\[)(.*?)(?=\\])"; String string = "sdsds [test] fdfddf"; Pattern pattern = Pattern.compile(regex); Matcher matcher = matcher.pattern(string) log.info(matcher.group(0)); but I get NO MATCHERS Have I done something wrong? – EdXX Jan 26 '19 at 21:50
  • You might wanna post another question – uneq95 Jan 26 '19 at 21:52