Regex Help 
Author Message
 Regex Help

Greetings:

I have the following expect code for getting the
name of the host:

#
# Set Hostname
#
expect * ;# Clear out expect buffer
set display "hostname"
send "$display\r"
expect {
    -re {(ATCA.+)[\r\n]+} { expect -re "$prompt" {}}
    default {Chk_Err $display; expect -re "$prompt" {}}

Quote:
}

set bladename $expect_out(1,string)
send_user "\nTCL Note: Hostname: $bladename\n"

The problem was that it kept getting carriage returns in the
name. Only when I changed the regex to:

    -re {(ATCA.+)\r}

Then it works properly. Change anyone explain why?

Thanks in Advance.

    Tom



Sat, 13 Dec 2008 00:43:26 GMT  
 Regex Help

[...]

Quote:
>  set display "hostname"
>  send "$display\r"
>  expect {
>      -re {(ATCA.+)[\r\n]+} { expect -re "$prompt" {}}
>      default {Chk_Err $display; expect -re "$prompt" {}}
>  }
>  set bladename $expect_out(1,string)
>  send_user "\nTCL Note: Hostname: $bladename\n"

>  The problem was that it kept getting carriage returns in the
>  name. Only when I changed the regex to:

>      -re {(ATCA.+)\r}

In the expression {(ATCA.+)[\r\n]+} the ".+" is greedier than [\r\n]+
Given the string "ACTA1234\r\n\r\n", then (ACTA.+) will match
"ACTA1234\r\n\r" and [\r\n]+ will match "\n"

This would work:  {(ATCA.+?)[\r\n]+}
But I tend to avoid using non-greedy quatifiers in Tcl because Tcl
regexes are either greedy or non-greedy
(http://groups.google.com/group/comp.lang.tcl/msg/c493317cc0d10d50?hl=en)

You probably want to make the ".+" part a bit more specific.  Do you
have any whitespace in any remote hostname?  Perhaps this expression
will work better:  expect -re {^ACTA\w+}

--
Glenn Jackman
Ulterior Designer



Sat, 13 Dec 2008 01:58:23 GMT  
 Regex Help

Quote:
> I have the following expect code for getting the
> name of the host:

> #
> # Set Hostname
> #
> expect * ;# Clear out expect buffer
> set display "hostname"
> send "$display\r"
> expect {
>     -re {(ATCA.+)[\r\n]+} { expect -re "$prompt" {}}
>     default {Chk_Err $display; expect -re "$prompt" {}}
> }
> set bladename $expect_out(1,string)
> send_user "\nTCL Note: Hostname: $bladename\n"

> The problem was that it kept getting carriage returns in the
> name. Only when I changed the regex to:

>     -re {(ATCA.+)\r}

> Then it works properly. Change anyone explain why?

With {(ATCA.+)[\r\n]+} you are (in effect) saying:

Match the literal text ATCA follow by at least one--but preferably as many
as possible--other character(s) followed by at least one--but preferably
as many as possible--of any combination of \r and \n.

By default regular expressions are "greedy" and so the earliest possible
greedy match will "win" and consume the input.

In the case where your input has \r\n the (ATCA.+) gets to consume the \r
(being greedy) because a lone \n will still match [\r\n]+.

The (ATCA.+) itself would like to take everything, including the \n, but
then the overall expression would fail to match on the [\r\n]+ case, so
the engine back tracks and gives \n to satisfy the [\r\n]+ requirement.

What you probably want instead is:

    -re {(ATCA[^\r\n]+)[\r\n]+}

Quote:
> Thanks in Advance.

>     Tom

Michael


Sat, 13 Dec 2008 02:24:44 GMT  
 Regex Help
Quote:

> What you probably want instead is:

>     -re {(ATCA[^\r\n]+)[\r\n]+}

Cool, thanks! I thought that by anchoring with the [\r\n] that would
limit the match, better read up about greediness in regex.

Tom



Sat, 13 Dec 2008 02:47:42 GMT  
 Regex Help

Quote:

> Greetings:

> I have the following expect code for getting the
> name of the host:

you've already got the answer to your question, but I'll add a bit of
a comment on another part of your script.

Quote:

> #
> # Set Hostname
> #
> expect * ;# Clear out expect buffer

This is not necessarily true.

It does clear the internal buffer (which could still be empty)
it does NOT cause expect to read more from the spawned process
since it will successfully match anything in the internal buffer,
including the null string.
if you really want to make sure any pending stuff is discarded,
you need to try to match something that you know will never match
as this will cause it to keep reading to try and make a match
then after the timeout (which should be fairly small if you don't
want unnecessary slowdown in your script) you do the expect *
to clear the internal buffer.

This is just a not, and probably doesn't matter in this case
since you are matching something fairly specific, but in
the general case of grabbing reults data and wanting to
make sure there is nothing there to start with this is a
useful tidbit.

bruce



Sat, 13 Dec 2008 04:47:43 GMT  
 Regex Help

Quote:

>> What you probably want instead is:

>>     -re {(ATCA[^\r\n]+)[\r\n]+}

> Cool, thanks! I thought that by anchoring with the [\r\n] that would
> limit the match, better read up about greediness in regex.

> Tom

I believe you misunderstand.  ^ as the first character in [...] is
a set inversion operator (match anything _except_ the set that
follows), not an anchor.  
--



Sat, 13 Dec 2008 07:37:20 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. RegEx help need!

2. Need regex help (or bug in match)

3. Regex Help Please

4. regex help please please !

5. regex help

6. Regex help

7. Regex help

8. regex help

9. regex help needed !

10. grep regex to ruby regex...

11. Regex++ or other Regex lib for Fortran?

12. Python regex / libc regex interactions

 

 
Powered by phpBB® Forum Software