Announcement

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

  • extracting numbers from macros

    Hello everyone

    Wondering if there is anyway to destring a set of somewhat irregular macros which comprise strings and numbers. Example below:
    Code:
    local a1="sds30wer"
    local a2="wweti256x"
    
    forvalues i=1/2{
    local n`i'=????? //how to extract the numbers from a1 and a2 so that correspondingly we get locals of n1=30, n2=256?
    }
    Thanks.

  • #2
    If there is only one number you could

    Code:
    local a1="sds30wer"
    local a2="wweti256x"
    
    forvalues i = 1/2 {
        if regexm("`a`i''", "([0-9]+)") {
            local n`i' = regexs(1) 
        }
        display "`n`i''"
    }
    Best
    Daniel

    Comment


    • #3
      This code will remove all characters that are not decimal digits.
      Code:
      local a1 "sds30wer"
      local a2 "wweti256x"
      local a3 "ab12ef78gh"
      
      forvalues i=1/3{
      local n`i' = ustrregexra("`a`i''","\D","")
      display "`a`i'' becomes `n`i''"
      }
      Code:
      sds30wer becomes 30
      wweti256x becomes 256
      ab12ef78gh becomes 1278

      Comment


      • #4
        Thanks a lot , Daniel!

        Comment


        • #5
          William Lisowski , that is cool! May I ask, apart from "\D", are there any other similar secret codes that may be useful? or where can I find a list of such codes? Thanks!

          Comment


          • #6
            http://userguide.icu-project.org/strings/regexp
            Last edited by Bjarte Aagnes; 23 Dec 2017, 12:01.

            Comment

            Working...
            X