Announcement

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

  • Combining OR (|) and 'end' ($) operator in ustrregexra()

    I have a list of (variable) names that I want to delete from a longer list of names using ustrregexra("long_list","short_list-regex",""), i.e. I'd like to search for all matches of short_list in long_list and replace them with nothing using ustrregexra().
    My problem is that I want to use the `$`-Operator to avoid matches with names that have additional suffixes.
    Here's an example:
    Suppose I have a global containing "te_m te_m_uq te_m_f te_m_uq_f" from which I'd like to delete te_m & te_m_uq, but keep te_m_f & te_m_uq_f - in my actual problem, both lists are quite a bit longer

    Code:
    glo long_list "te_m te_m_uq te_m_f te_m_uq_f"
    
    . dis ustrregexra("${long_list}","(te_m|te_m_uq)","X")  // not what I want -> leave 2 & 4 unchanged
    X X_uq X_f X_uq_f
    
    . dis ustrregexra("${long_list}","(te_m$|te_m_uq$)","X") // does not change te_m & te_m_uq
    te_m te_m_uq te_m_f te_m_uq_f
     
    . dis ustrregexra("${long_list}","(te_m|te_m_uq)$","X") // does not change te_m & te_m_uq
    te_m te_m_uq te_m_f te_m_uq_f
    The result should eventually be stored into a global

  • #2
    Use word boundaries to match exact words and locals instead of globals.

    Code:
    local long_list "te_m te_m_uq te_m_f te_m_uq_f"
    display ustrregexra("`long_list'","\b(te_m|te_m_uq)\b","X")
    Res.:

    Code:
    . display ustrregexra("`long_list'","\b(te_m|te_m_uq)\b","X")
    X X te_m_f te_m_uq_f
    
    .

    Comment


    • #3
      Perfect, works like a charm - thank you Andrew Musau!

      Comment

      Working...
      X