2

I am looking for a regular expression to generate acronym from any string. Say I have a string like "Apple Banana Cake" then the result is "ABC". Another example is "1 Banana 2 Orange" then the result is "1B2O"

I tried as given below as mentioned here - How to get the acronym from any string? but that does not work as expected.

writeDump(reReplaceNoCase('Apple Banana Carrot','[\\B.|\\P{L}]','','ALL'));
writeDump(reReplaceNoCase('Apple Banana Carrot','[\\B.|\\P]','','ALL'));
to-find
  • 33
  • 3
  • Try `'\B[a-zA-Z]|[^a-zA-Z]'` – Wiktor Stribiżew Aug 09 '21 at 16:47
  • I tried it but it does not work for string like this "1 Banana 2 Orange". Here result should be 1B2O. – to-find Aug 09 '21 at 17:18
  • Did you try it with a single backslash without the square brackets? `\B.|\P{L}` https://regex101.com/r/hE3YA8/1 Or use [match](https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/rematch.html) with `(?<!\S)[0-9A-Z]` and join the values to a string https://regex101.com/r/44LdAk/1 – The fourth bird Aug 09 '21 at 17:35
  • 3
    Do you want to create an initialism from a string, or do you want to create an initialism from a string _with a regex_? IE: is it significant that the solution uses a regex operation? You can just do it a split / map / join in one expression. – Adam Cameron Aug 09 '21 at 18:24
  • 1
    An example of what Adam was saying (I'm sure someone can come up with a better implementation): function acronym(list) {return ucase(arrayToList(arrayMap(listToArray(list," "), function(item){ return Left(Trim(item),1);}),""));} – Steve Bryant Aug 09 '21 at 19:15
  • 1
    @SteveBryant that is *very* close to what I was going to suggest! (https://trycf.com/gist/34c30d2f1408feb1bee7dad048749f93/acf2021?theme=monokai) – Adam Cameron Aug 09 '21 at 20:17
  • @AdamCameron is it a Java stream like syntax. Also trying to understand , why arrayNew(1) in first place ? – to-find Aug 10 '21 at 03:30
  • It's a work around to convert `s.split("\s+")` - which returns a Java String array - to a CFML array so one can call `map` on it. `arrayNew(1).append()` will accept a Java String array as its argument. Appending that array to an empty array just results in a CFML array of strings. – Adam Cameron Aug 10 '21 at 07:19
  • @AdamCameron wondering how we can directly call java`split()` method on a coldfusion string? How is it possible? curious to know! And , `s.split()` is available from which CF version onwards ? – to-find Aug 12 '21 at 10:11
  • CFML strings *are* java.lang.Strings (since CFMX6). So unless yer running CF5, you should be fine to just do it. – Adam Cameron Aug 12 '21 at 15:29

1 Answers1

2

You can use

writeDump(reReplaceNoCase('Apple Banana Carrot','\B[a-zA-Z]|[^0-9a-zA-Z]','','ALL'));

See the regex demo. Details:

  • \B[a-zA-Z] - an ASCII letter that is preceded with a word char
  • | - or
  • [^0-9a-zA-Z] - any non-alphanumeric char from the ASCII set.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563