regexp — {b?} abcd match

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf regexp — {b?} abcd match

  • Creator
    Topic
  • #50808
    Bob Schmid
    Participant

    the above command comes back with one hit but no value…..? why

    non-greedy search…shouldn’t it find the “b” ?

    tcl>regexp — {b?} abcd match

    1

    tcl>echo $match

    tcl>

    tcl>regexp — {a?} abcd match

    1

    tcl>echo $match

    a

    tcl>

    Come on Charlie….lay it on me….Im almost afraid to ask!

Viewing 7 reply threads
  • Author
    Replies
    • #67595

      What are you trying to do exactly?

      The return value of “1” means it did match on your expression.

      I’m actually not sure what regexp stores in the match variable. It wasn’t clear, at least to my eyes, in the documentation … “match will be set to the range of string that matched all of exp.

      If you want to capture the value, you’ll need to use parens and variables like this: regexp — {(b)} abdc match var1; puts $var1.

      If you want to use the expression in a decision, do it like this: if {[regexp {b} abdc]} {…}.

      -- Max Drown (Infor)

    • #67596
      Tom Rioux
      Participant

      Not sure what you are trying to do but one way (provided the pattern you are looking for isnt at the beginning or the end of a word),

      hcitcl>regexp — {bY} abcd match

      1

      hcitcl>echo $match

      b

      What ARE you trying to do?

      Tom

    • #67597
      Bob Schmid
      Participant

      we are going thru some regexp examples….understanding greedy vs non-greedy…and was wondering if in fact the above got a hit…..what did it get a hit on ?

    • #67598

      Took me years of experience to get a good grasp on greediness.

      -- Max Drown (Infor)

    • #67599

      Robert Schmid wrote:

      if b? is greedy (match as much as possible) why did it not find the b in “abcd”..

      It *DID* find it. That’s why it returned a “1”.

      -- Max Drown (Infor)

    • #67600

      Oh. I think I see your problem. The pattern “b?” means 0 or 1 b’s. You have to use a modifier like . * + for greediness modifications.

      Ex. b*?, b+?

      Code:

      regexp — {b} abcd match
      puts $match
      >b
      regexp — {a} abcd match
      puts $match
      >a

      -- Max Drown (Infor)

    • #67601
      David Barr
      Participant

      Max Drown wrote:

      It *DID* find it. That’s why it returned a “1”.

      Maybe it didn’t find the “b”.  This expression returns a one as well:

      Code:

      regexp — {b?} acd match

      That’s because the question mark means to match zero or one occurrences of the previous character.  The pattern {b?} will always match any string.  

      Robert, I think that the reason you’re seeing this behavior is due to confusion about what greedy means.  I think that it means that if it finds a match, it will try to use as many characters around that match as possible.  However, if there are multiple matches, a greedy operator won’t match the longest one, it will just match the first one.  In your case, every single character in the string matches your pattern, so it tries to match the first character, a.  The greedy nature of the expression causes it to evaluate if it can match one occurance of “b” against the “a” instead of zero, but that fails.

      You’ll see slightly different results with this test:

      Code:

      regexp — {b?} bcde match

      In this case, a “b” will be stored in match.

    • #67602
      Charlie Bursell
      Participant

      Don’t you just hate it when it does exactly what you told it to do?  🙄

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

Forum Statistics

Registered Users
5,042
Forums
28
Topics
9,200
Replies
34,023
Topic Tags
267