1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

=~ ... still don't get it

Discussion in 'Support' started by vefatica, Jun 14, 2011.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,934
    Likes Received:
    30
    I'm having a tough time understanding how the =~ test works.

    Code:
    v:\> if de =~ e+$  echo yes
    yes
    
    v:\> if "de" =~ "e+$"  echo yes
    
    v:\> if "a b c" =~ ". . ." echo yes
    yes
    
    v:\> if "a b c" =~ ". ." echo yes
    
    v:\>
    Above, the first is expected (?). Why not the same result in the second? [In my experience, both strings should be un-quoted before handing them off to Oniguruma]. The fact that the first works suggests that any match found yields a positive result. Then why is the fourth one above negative?
     
  2. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    559
    Likes Received:
    7
    My guess is that it is using the double-quotes as part of the regex and
    Code:
    ". ." ( dquote anychar space anychar dquote )
    is not found.

    Using variables to hold the values works
    Code:
    [J:\]set test1=a b c
    
    [J:\]set test2=. .
    
    [J:\]if %test1 =~ %test2 echo yes
    yes
    
    Try using grouping (parentheses) instead of double-quotes
    Code:
    [J:\]if "a b c" =~ (. .) echo yes
    yes
    
    [J:\]if "a b c" =~ (?:. .) echo yes
    yes
    
    [J:\]if "a b c" =~ (?:.  .) echo yes
    
    [J:\]
    
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,934
    Likes Received:
    30
    On Tue, 14 Jun 2011 12:50:03 -0400, JohnQSmith <> wrote:

    |My guess is that it is using the double-quotes as part of the regex

    The regex may **need** to be quoted *for TCC's sake) because of spaces or
    special characters; the same for the string.

    Suoopse you want to test whether a filename (maybe with spaces) has the form
    .*\d\d\.txt (any text, two digits, .txt). If the quotes are left on the string
    (filename) then the regex must look for them ... cumbersome because it must be
    escaped fot TCC and for the regex.

    Code:
    v:\> if "abc 55.txt" =~ \d\d\.txt\^"$ echo yes
    yes
    
    v:\> if "abc 55.txts" =~ \d\d\.txt\^"$ echo yes
    
    v:\>
    And as noted, that fails if the regex is quoted.

    Code:
    v:\> if "abc 55.txt" =~ "\d\d\.txt\^"$" echo yes
    
    v:\>
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    IF / IFF do not remove double quotes. They are not delimiters, they are part of the arguments. (See CMD.EXE.)
     
  5. dcantor

    Joined:
    May 29, 2008
    Messages:
    507
    Likes Received:
    3
    and the help file needs help, too

    Also, a help non-nit (at least from my perspective) is that the new !~ and =~ should have index entries in the help (like != and == do).
     

Share This Page