-2

I need to split an input and put it in to a list according to a list of keywords or delimiters that I have created.

I've tried simply splitting the string by spaces and then processing it afterwards, but the problem is that some of my keywords have spaces in them so they are split apart which causes undesired behavior.

// "Jr.", "III", "Sr", "pro se" are all keywords in my list
String input = "Abraham Lincoln Jr. III Sr pro se";
String [] splitBySpace = input.split(" ");
List<String> separatedName = new ArrayList<>(Arrays.asList(splitBySpace);

My list ends up being: {Abraham, Lincoln, Jr., III, Sr, pro, se} And I would like it to be: {Abraham, Lincoln, Jr., III, Sr, pro se} This output would also work: {Abraham Lincoln, Jr., III, Sr, pro se} (I don't need the strings that are not in my delimiter list to be split apart)

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Kev K
  • 17
  • 2
  • Use a regex in `split()` and see https://stackoverflow.com/questions/12636417/splitting-a-string-using-words-as-delimiters – Redoishi Jan 31 '19 at 14:58
  • `// "Jr.", "III", "Sr", "pro se" are all keywords in my list`. Can you show content of your list? – anubhava Jan 31 '19 at 15:00
  • You cannot expect to get help here unless you provide all details of your problem. – anubhava Jan 31 '19 at 16:26

2 Answers2

0

You could use this: \s(?=(Jr\.|III|Sr|pro se)) - and add in the keywords you want to preserve.

Here's a working demo to see what it matches: https://regexr.com/47gbm

So with String[] split = input.split("\\s(?=(Jr\\.|III|Sr|pro se))"); you should get the desired result.

bkis
  • 2,530
  • 1
  • 17
  • 31
  • My delimiter list is pretty long so could I use a String variable in the regex instead of hardcoding in Jr\\.|III|Sr|pro se|... – Kev K Jan 31 '19 at 15:17
  • Sure, you could. But it's not very pretty nor performant. If it's a one-time process and performance isn't critical, it might be okay. – bkis Jan 31 '19 at 15:20
  • So would it look like: `String[] split = input.split("\\s(?=(regex))");` – Kev K Jan 31 '19 at 15:28
0

You can try adding a different seprator to the string hoding the keywords. Insted of seprating them by " " separate them by "|".

String input = "Abraham|Lincoln|Jr.|III|Sr|pro se";
String [] splitByPipe = input.split("\\|");

This way you won`t be having any problems with the spaces you need to keep.

S.Tushinov
  • 548
  • 5
  • 13
  • But to do that, you have to know where your keywords are in the target string. Same problem as before, isn't it? – bkis Jan 31 '19 at 15:21