REGEXP help with anchors

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf REGEXP help with anchors

  • Creator
    Topic
  • #54933
    Mike Wilson
    Participant

      I am having some issues with getting the matches I want.  Or dont want more specifically.  

      Here is what I am doing.  

      I have fields that I am adding into a list.  

      set list

        when I echo the list i get:

        DR^NAME^HERE {MORE^DR^NAMES^N^^^MD~THIS^DOC^NAMES^P^^^MD}

        I then try and run a regexp against them:

        lsearch -regexp $list “HI”

        This matches due to the “THIS” in the second part of the list.  However, I dont want it to.  I want it to match only if HI is by itself.  

        If I take the regexp outside of tcl I can do anchors such as HIb .  Then it will only match if the list is

        DR^NAME^HERE {MORE^DR^NAMES^N^^^MD~HI^DOC^NAMES^P^^^MD}

        How do I get this regexp to work inside of TCL?  Thanks in advance for any help!

    Viewing 2 reply threads
    • Author
      Replies
      • #83506
        Rehman Masood
        Participant

          Try this,

          lsearch -regexp $list “^HI$”

        • #83507
          Keith McLeod
          Participant

            You could also use something like “[~^]HI[~^]”

            This would qualify it as being preceded or followed by a ~ or ^ character. Nothing else should make this true.

          • #83508
            Charlie Bursell
            Participant

              ^ only matches at the beginning of a string and $ at the end

              the expr:

              lsearch -inline -regexp $list {mHIM}

              m says match only at beginning of a word while M matches at the end of a word

              From lsearch man page:

              -regexp

              Pattern is treated as a regular expression and matched against each list element using the rules described in the re_syntax reference page.

              re-syntax:

              NAME

              re_syntax – Syntax of Tcl regular expressions.

              DESCRIPTION

              DIFFERENT FLAVORS OF REs

              REGULAR EXPRESSION SYNTAX

              *

              +

              ?

              {m}

              {m,}

              {m,n}

              *? +? ?? {m}? {m,}? {m,n}?

              (re)

              (?:re)

              ()

              (?:)

              [chars]

              .

              k

              c

              {

              x

              ^

              $

              (?=re)

              (?!re)

              BRACKET EXPRESSIONS

              ESCAPES

              a

              b

              B

              cX

              e

              f

              n

              r

              t

              uwxyz

              Ustuvwxyz

              v

              xhhh

              xy

              xyz

              d

              s

              w

              D

              S

              W

              A

              m

              M

              y

              Y

              Z

              m

              mnn

              METASYNTAX

              b

              c

              e

              i

              m

              n

              p

              q

              s

              t

              w

              x

          Viewing 2 reply threads
          • The forum ‘Cloverleaf’ is closed to new topics and replies.