Announcement

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

  • Regular expression help

    Hello,

    Apologies for making a second regular expressions-related post today, but the topic is new to me and I've struggled to answer my current problem with the info online.

    I have the following string values, say, for variable Branching:

    Code:
    st12a(5)==1 & st13b == 3
    st8a(88) == 1
    (e1 == 1  | e1 == 2) & e2==0
    and I want to convert the (1 or 2-digit) number in the parentheses that may be followed by " ==" or "==" to two underscores and the number without parentheses :

    Code:
    st12a__5==1 & st13b == 3
    st8a__88 == 1
    (e1 == 1  | e1 == 2) & e2==0
    (notice last value unchanged)


    Any help?

    Thanks a lot,
    Reese

    v 14.2

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str28 text
    "st12a(5)==1 & st13b == 3"    
    "st8a(88) == 1"              
    "(e1 == 1  | e1 == 2) & e2==0"
    "st8a(88) != 1"              
    end
    
    generate str40 new = ustrregexra(text,"\((\d+)\)( *==)","__$1$2")
    list, clean noobs
    Code:
    . list, clean noobs
    
                                text                            new  
            st12a(5)==1 & st13b == 3       st12a__5==1 & st13b == 3  
                       st8a(88) == 1                  st8a__88 == 1  
        (e1 == 1  | e1 == 2) & e2==0   (e1 == 1  | e1 == 2) & e2==0  
                       st8a(88) != 1                  st8a(88) != 1
    Regarding documentation, to the best of my knowledge, only in the Statlist post linked here is it documented that Stata's new regular expression parser is the ICU regular expression engine documented at http://userguide.icu-project.org/strings/regexp.

    Last edited by William Lisowski; 01 Mar 2019, 14:47.

    Comment


    • #3
      So helpful--thanks again, William!

      Comment

      Working...
      X