Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Removing all caps words from string variable

    Hello, I am looking for help with syntax that will remove any all caps words from a string variable "about" but does not remove a word if just one letter is capitalized. For example, in the sentence, "I NEED help" the word "NEED" would be removed but "I" would not. Can anyone help with this?

  • #2
    I is usually capitalized and is one word. You may want to enforce the restriction that the to be removed word is at least 2 characters long.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str11 text
    "I need help"
    "I NEED help"
    "I Need Help"
    "I NEEd hELP"
    end
    
    gen wanted= trim(itrim(ustrregexra(" "+text+ " ", "\s[A-Z]{1}[A-Z]+\s", " ")))
    Res.:

    Code:
    . l
    
         +---------------------------+
         |        text        wanted |
         |---------------------------|
      1. | I need help   I need help |
      2. | I NEED help        I help |
      3. | I Need Help   I Need Help |
      4. | I NEEd hELP   I NEEd hELP |
         +---------------------------+
    Last edited by Andrew Musau; 04 Jun 2023, 08:21.

    Comment


    • #3
      This is perfect. Thank you!!

      Comment


      • #4
        regex using unicode property “Uppercase Letter” (Lu):
        Code:
        \s\p{Lu}{2,}\s

        Comment

        Working...
        X