Loading ...
Sorry, an error occurred while loading the content.

match(), regexp problem

Expand Messages
  • Zdenek SEKERA
    I am ashamed but I can t figure this out: 012345678 let z= a[q]x[qq] 1. echo match(z, [* ] ) returns 3 as expected (beginning of the pattern) 2. echo
    Message 1 of 3 , May 3 6:27 AM
    • 0 Attachment
      I am ashamed but I can't figure this out:

      012345678
      let z="a[q]x[qq]"

      1. echo match(z, '\[*\]')
      returns 3 as expected (beginning of the pattern)

      2. echo match(z, '\[*\]$')
      returns 8, I expected 3 as well
      why does it return *end* of the pattern??
      All I tried to tell it was the closing bracket ']'
      has to be the last char on the line.

      3. actually, I found why:

      echo matchstr(z, '\[*\]$')
      returns ']' which is indeed the 8th char.
      What did it do with the rest of my regexp?
      I wanted to get all between [...], closing
      bracket to be last on the line, so I expected
      '[q]x[qq]'.

      4. The real problem is this:
      I want to get the beginning (using match()) char
      of the expression [...], closing bracket as last char
      but the minimum chars (regexp should not be greedy).
      In the example above I would like to get 5, and the matchstr
      should return '[qq]'.

      Anyone can help? Thanks and cheers,

      ---Zdenek
    • Milan Vancura
      ... It is also the end of the pattern! And the beginning of the pattern as well, because your regexp matches exactly ONE character. You probably wanted to
      Message 2 of 3 , May 3 6:39 AM
      • 0 Attachment
        > 1. echo match(z, '\[*\]')
        > returns 3 as expected (beginning of the pattern)

        It is also the end of the pattern! And the beginning of the pattern as well,
        because your regexp matches exactly ONE character. You probably wanted to
        write:

        echo match(z, '\[.*\]')

        Hope this helps.

        Milan Vancura
      • Zdenek Sekera
        ... ... ... Where did my . go ?? I knew I must not see something obvious! ... Beauty, thanks!
        Message 3 of 3 , May 3 9:25 AM
        • 0 Attachment
          > > 012345678
          > > let z="a[q]x[qq]"
          ...
          ...
          > > echo matchstr(z, '\[*\]$')
          > > returns ']' which is indeed the 8th char.
          > > What did it do with the rest of my regexp?
          > > I wanted to get all between [...], closing
          > > bracket to be last on the line, so I expected
          > > '[q]x[qq]'.
          >
          > Possibly what you want is
          > echo matchstr(z,'\[.*]$')
          >

          Where did my . go ??
          I knew I must not see something obvious!

          ...
          > Then I suppose you want
          >
          > echo matchstr(z,'\[[^[]*]$')

          Beauty, thanks!

          ---Zdenek
        Your message has been successfully submitted and would be delivered to recipients shortly.