Abbreviate

So I have the expression rule that will abbreviate a string. Thoughts on how to improve?

 

/*Replace all special characters with spaces*/
/*Replace extra spaces with since space*/
/*Wrap numbers in spaces*/
/*Split words and number sets into array*/
/*ForEach item in array grab number or first letter of word*/
/*Join what is left, upper case string and grab left 50 characters*/
with(
  local!nospecialcharacters: regexreplaceall(
    "[^\w\s]",
    ri!in,
    " "
  ),
  local!extraspaceswithsinglespace: regexreplaceall(
    "\s+",
    local!nospecialcharacters,
    " "
  ),
local!numberwrapper: 
  if(regexmatch("\d+",local!extraspaceswithsinglespace),
  
  regexinsertmatchmarkers(
    "\d+",
    local!extraspaceswithsinglespace,
    " ",
    " ",
    false
  ),
  local!extraspaceswithsinglespace
  
  ),
  local!segments: split(
    regexreplaceall(
      "\s+",
      local!numberwrapper,
      "|"
    ),
    "|"
  ),
  local!keepers: a!forEach(
    local!segments,
    if(
      isnull(
        fv!item
      ),
      null,
      if(
        not(
          isnull(
            tointeger(
              fv!item
            )
          )
        ),
        fv!item,
        if(
          fv!item = " ",
          null,
          if(
            regexmatch("^[a-zA-Z]+$",
              fv!item
            ),
            left(
              fv!item,
              1
            ),
            null
          )
        )
      )
    )
  ),
  left(
    upper(
      joinarray(
        local!keepers,
        ""
      )
    ),
    50
  )
)

  Discussion posts and replies are publicly visible

Parents
  • Hello Rick, 

    Now answering your question.

    When I first saw your post I thought something simple like this:  (assuming that your first letter of the product is UpperCase and the rest are lowercase, if this works for you, maybe do this as simple as this regex)  =D

     

    with(
      local!regex: "[^A-Z0-9]",
      regexreplaceall(local!regex,ri!in,"")
    )
    
    

    In case you cannot ensure this maybe this will work for you:

    (here I am splitting the text and then getting the first letter of each making it uppercase and then making what I did in the first option I gave )

    with(
      local!regex: "[^A-Z0-9]",
      local!text: "Product 123 Ve3ndor 2ASD3",
      local!separator:"",
      
      joinarray(
        a!forEach(
          items: split(
            local!text,
            " "
          ),
          expression: regexreplaceall(
            local!regex,
            upper(
              fv!item[1]
            ) & lower(
              mid(
                fv!item,
                2,
                len(
                  fv!item
                )
              )
            ),
            ""
          )
        ),
        local!separator
      )
    )

     

    Please let me know what you think. You can remove all the special characters and everything at once. given you example Product 123 Vendor 098 the first option works perfectly. 

     

    Jose Perez

Reply
  • Hello Rick, 

    Now answering your question.

    When I first saw your post I thought something simple like this:  (assuming that your first letter of the product is UpperCase and the rest are lowercase, if this works for you, maybe do this as simple as this regex)  =D

     

    with(
      local!regex: "[^A-Z0-9]",
      regexreplaceall(local!regex,ri!in,"")
    )
    
    

    In case you cannot ensure this maybe this will work for you:

    (here I am splitting the text and then getting the first letter of each making it uppercase and then making what I did in the first option I gave )

    with(
      local!regex: "[^A-Z0-9]",
      local!text: "Product 123 Ve3ndor 2ASD3",
      local!separator:"",
      
      joinarray(
        a!forEach(
          items: split(
            local!text,
            " "
          ),
          expression: regexreplaceall(
            local!regex,
            upper(
              fv!item[1]
            ) & lower(
              mid(
                fv!item,
                2,
                len(
                  fv!item
                )
              )
            ),
            ""
          )
        ),
        local!separator
      )
    )

     

    Please let me know what you think. You can remove all the special characters and everything at once. given you example Product 123 Vendor 098 the first option works perfectly. 

     

    Jose Perez

Children
No Data