0

I have problem with my tcl script. In one part of code I grep one string form xml.file containing ipadress (see below), and now I need to match the expression with expect -re i next line, so that I can save it to varible. So I need your help, how to match this, see below what I mean. This is part of code in TCL:

  send -- "cat $filename | grep 'url' \r"
  expect -re "(\<emUrl url=\"(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\"\/\>\).*ranosusr@rn2osscs603>$"
   set IP $expect_out(1,string)

probelm is that I don't know how to match this experssion, wich is the result of grep:

<emUrl url="10.80.31.123"/>

the ipaddress should be general, because they are different from file to file.

With exp_internal 1 I get this ouput: end: sending "cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url' \r" to { exp4 }

expect: does "> " (spawn_id exp4) match regular expression "(<emUrl url="(.*)").*ranosusr@rn2osscs603>$"? no
cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url'

expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url' \r\r\n" (spawn_id exp4) match regular expression "(<emUrl url="(.*)").*ranosusr@rn2osscs603>$"? no
                  <emUrl url="10.80.31.123"/>

expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url' \r\r\n                  <emUrl url="10.80.31.123"/>\r\r\n" (spawn_id exp4) match regular expression "(<emUrl url="(.*)").*ranosusr@rn2osscs603>$"? no
ranosusr@rn2osscs603>
expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url' \r\r\n                  <emUrl url="10.80.31.123"/>\r\r\nranosusr@rn2osscs603> " (spawn_id exp4) match regular expression "(<emUrl url="(.*)").*ranosusr@rn2osscs603>$"? no
expect: timed out

expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url' \r\r\n                  <emUrl url="10.80.31.123"/>\r\r\nranosusr@rn2osscs603> " (spawn_id exp4) match regular expression "(<emUrl url="([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})"/>).*ranosusr@rn2osscs603>$"? no
expect: timed out

so take a look at my code, I think maybe some brackets are needed that I can't figure out. Also I have to include ranosusr@rn2osscs603> because this is the server prompt. Please help :)

1 Answer 1

2

As far as I can see (looking at that log, carefully) the main problem with your regular expression is that it contains an End-Of-Text/Line anchor ($) yet at no point does the string it is matched against ever contain that; either it doesn't have the text before that anchor in your RE, or it has a space after it. At the very least, try \s$ (well, \\s$) instead so that the space doesn't cause problems.

You might consider using non-greedy REs instead to simplify that RE. Here is what I think would work (without Tcl-syntax backslashes; put it in {braces} to give this to expect without having to inflict backslash-itis on it).

(<emUrl url="(.*?)"/>).*ranosusr@rn2osscs603>\s*$

If you need a reference to the RE language variant supported by Tcl, check out the re_syntax(n) manual page.


OK, $ is normally an EOT anchor by default, but Expect forces the RE engine to be in EOL mode. This is the Right Thing.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi , I used this template and it's working fine :expect -re {<emUrl url="(.*?)"/>} .....what does command (.*?) replace, is it all what is inside ""?
(.*?) does match (and capture) the minimum amount that it can, as opposed to (.*) which captures the maximum. The stuff around it constrains what that minimum is; for this sort of thing, it's often Good Enough even if it isn't actually Formally Correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.