Re: How to use [[ string =~ regexp ]]?

2006-05-22 Thread Peter Volkov
On Вск, 2006-05-21 at 15:55 -0400, Paul Jarc wrote:
 [a-z] matches only one charater, but the pattern is not required to
 match against the entire string. 

On Вск, 2006-05-21 at 13:57 -0600, Mike Stroyan wrote:
 The =~ regexp match will match a substring by default.  You can use ^ and $
 to anchor the expression to the start and end of the string.

Yes! That was what I missed.

On Вск, 2006-05-21 at 22:40 -0400, Chet Ramey wrote: 
 It seems reasonable that quoting any part of the rhs to the =~
 operator should cause it to behave in the same manner.
 
 Since the arguments to [[ don't undergo any of the expansions that
 require quoting to protect them, there's no reason for =~ to act
 differently than the other operators that do pattern matching.

Never noticed that. This is really interesting.

Thank you all for your answers,
Peter.


signature.asc
Description: This is a digitally signed message part
___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


How to use [[ string =~ regexp ]]?

2006-05-21 Thread Peter Volkov
Hello.

Please CC my email to answers as I'm not subscribed to the list.

I have problems using =~ operator. I've tried to search for answer, but
failed. I'm using GNU bash, version 3.1.17. Can anybody give me some
examples of usage?

I really do not understand why

$ [[ string =~ [a-z] ]]  echo something
something

echo me something. IIUC the regular expression [a-z] matches any single
letter, so how string string matches one letter?

Seems that I missed the point, or did I encounter bug?

Thank you for any help,
Peter.


signature.asc
Description: This is a digitally signed message part
___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Paul Jarc
Peter Volkov [EMAIL PROTECTED] wrote:
 $ [[ string =~ [a-z] ]]  echo something
 something

[a-z] matches only one charater, but the pattern is not required to
match against the entire string.  You can force it to match the whole
string by using ^ to anchor the pattern to the beginning of the
string, and $ to anchor it to the end:
[[ string =~ ^[a-z]$ ]]


paul


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Mike Stroyan

On 5/21/06, Peter Volkov [EMAIL PROTECTED] wrote:


I have problems using =~ operator. I've tried to search for answer, but
failed. I'm using GNU bash, version 3.1.17. Can anybody give me some
examples of usage?

I really do not understand why

$ [[ string =~ [a-z] ]]  echo something
something

echo me something. IIUC the regular expression [a-z] matches any single
letter, so how string string matches one letter?


The =~ regexp match will match a substring by default.  You can use ^ and $
to anchor the expression to the start and end of the string.  You
won't get a match
with
[[ string =~ ^[a-z]$ ]]  echo match
But you will get a match with
[[ string =~ ^[a-z]{6}$ ]]  echo match
because it matches the correct number of characters.

--
Mike Stroyan
[EMAIL PROTECTED]


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Chet Ramey
Peter Volkov wrote:
 Hello.
 
 Please CC my email to answers as I'm not subscribed to the list.
 
 I have problems using =~ operator. I've tried to search for answer, but
 failed. I'm using GNU bash, version 3.1.17. Can anybody give me some
 examples of usage?
 
 I really do not understand why
 
 $ [[ string =~ [a-z] ]]  echo something
 something
 
 echo me something. IIUC the regular expression [a-z] matches any single
 letter, so how string string matches one letter?
 
 Seems that I missed the point, or did I encounter bug?

regexec(3) returns success for that pattern, so the [[ command succeeds.
I'm not sure why, unless it thinks it doesn't have to match the entire
string.

The real question is whether or not quoting the pattern should work as
it does with other [[ pattern matching operators, which is to quote any
characters special to the matching engine.  I think it should, for
consistency.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Chet Ramey
Paul Jarc wrote:
 Peter Volkov [EMAIL PROTECTED] wrote:
 $ [[ string =~ [a-z] ]]  echo something
 something
 
 [a-z] matches only one charater, but the pattern is not required to
 match against the entire string.  You can force it to match the whole
 string by using ^ to anchor the pattern to the beginning of the
 string, and $ to anchor it to the end:
 [[ string =~ ^[a-z]$ ]]

Paul is correct; that is the piece I was missing (I am distracted by
the Cleveland-Detroit game).

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Bob Proulx
Chet Ramey wrote:
 Peter Volkov wrote:
  Please CC my email to answers as I'm not subscribed to the list.
  $ [[ string =~ [a-z] ]]  echo something
  something
 ...
 The real question is whether or not quoting the pattern should work as
 it does with other [[ pattern matching operators, which is to quote any
 characters special to the matching engine.  I think it should, for
 consistency.

Could you give an example of how you think it should work?  (Just
curious and trying to keep up.)

Are you thinking that this should match:

  [[ string =~ [a-z] ]]  echo matched || echo not matched

Which this should not match?

  [[ string =~ [a-z] ]]  echo matched || echo not matched

Thanks
Bob


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Chet Ramey
Bob Proulx wrote:
 Chet Ramey wrote:
 Peter Volkov wrote:
 Please CC my email to answers as I'm not subscribed to the list.
 $ [[ string =~ [a-z] ]]  echo something
 something
 ...
 The real question is whether or not quoting the pattern should work as
 it does with other [[ pattern matching operators, which is to quote any
 characters special to the matching engine.  I think it should, for
 consistency.
 
 Could you give an example of how you think it should work?  (Just
 curious and trying to keep up.)

The documentation for [[ says this about the == and != operators,
which take patterns as the right-hand side:

Any part of the pattern may be quoted to force it to
be matched as a string.

It seems reasonable that quoting any part of the rhs to the =~
operator should cause it to behave in the same manner.

Since the arguments to [[ don't undergo any of the expansions that
require quoting to protect them, there's no reason for =~ to act
differently than the other operators that do pattern matching.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
   Live Strong.  No day but today.
Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://cnswww.cns.cwru.edu/~chet/


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash


Re: How to use [[ string =~ regexp ]]?

2006-05-21 Thread Bob Proulx
Chet Ramey wrote:
 It seems reasonable that quoting any part of the rhs to the =~
 operator should cause it to behave in the same manner.
 
 Since the arguments to [[ don't undergo any of the expansions that
 require quoting to protect them, there's no reason for =~ to act
 differently than the other operators that do pattern matching.

That makes sense to me.  Thanks for the clarification and education.

Bob


___
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash