0

I have a String that I've retrieved from conventions set by get methods. Something like getMyObject which becomes MyObject. I want to make this String presentable to the user so "My Object". However, I also want to account for acronyms that could occur within the object name, so something like getFBILicense should become "FBI License". I'm a little stuck on how to do this with regex.

(?=\\p{Upper}+\\p{Lower})

The above feels like what I'm after, and yet it's not quite right, this breaks for the simple example "thisIsMMyStringValue" which becomes this is M My String Value. I think this error makes sense to me, as both M and My are an uppercase with 0 or more uppercases between them and the first lowercase they encounter. However, I am not sure how to address this using regex so that the returned value is instead this is MMy String Value

EDIT: I am using Java's String::split method with the regex in question.

mike
  • 1,318
  • 3
  • 21
  • 41
  • Forward lookup to see if the next letter is also capitalized would work, have you tried that? – ranu Mar 08 '16 at 17:12
  • It looks like your code is right; your example 'getFBILicense' produces 'FBI License' - this is the same as with 'MMy' becoming 'M' and 'My'. What's wrong with that? If you were to get 'MMy' from 'MMy' then you are breaking your acronym rule because there's a lowercase in it.. – Kenney Mar 08 '16 at 17:14
  • You correctly pointed out that MMy is not a good example, however `FBILicense` returns `F` `B` `I` `License` under the given conditions. – mike Mar 08 '16 at 17:16
  • See the linked question, this (atrocity) works: `(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])` – Tunaki Mar 08 '16 at 17:17
  • The atrocity does indeed work – mike Mar 08 '16 at 17:21
  • What if method name is `getMyName1Name2Name3` should that not be translated to `My Name1 Name2 Name3`? – anubhava Mar 08 '16 at 17:52

0 Answers0