Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Regex check

by spivey49 (Monk)
on Aug 14, 2008 at 23:08 UTC ( #704443=perlquestion: print w/replies, xml ) Need Help??

spivey49 has asked for the wisdom of the Perl Monks concerning the following question:

Can someone confirm my regex for a Net::Telnet prompt? I need to match a command prompt like 'any text->'

What I have is this:

prompt => '/[\w().-]*\(?([\w.-])?\)?\s*->\s$/'

Looks correct to me, but trying to set that prompt is throwing an error in Net::Telnet for some reason

Replies are listed 'Best First'.
Re: Regex check
by toolic (Bishop) on Aug 15, 2008 at 00:51 UTC
    Deciphering your optical illusion... er, regular expression is a job for YAPE::Regex::Explain:
    use warnings; use strict; use YAPE::Regex::Explain; my $re = '[\w().-]*\(?([\w.-])?\)?\s*->\s$'; print YAPE::Regex::Explain->new($re)->explain(); __END__

    Do you really need those capturing parentheses? All the examples in the doc show pretty simple regexes for prompt. Maybe you could try simplifying the regex until it passes some simple case.

    is throwing an error
    What is the error message?

      Ah, thank you! I had forgoten all about YAPE::Regex::Explain.

      The capturing parentheses are needed to pass the expression to a different function.

      What is the error message?

      I'm not entirely sure yet why it's throwing an error. It's an internal error check with Net::Telnet. After much debuging I've determined errmode->mode is being set to die after passing this regex to the prompt method. This causes another function, error(), to exit the script with die, but it doesn't print the error message. I was hoping maybe there was something I was missing with the regex causing the behavior

Re: Regex check
by GrandFather (Sage) on Aug 14, 2008 at 23:40 UTC

    Should the last part be ->\s*$/' perhaps (note the \s*)>

    Perl reduces RSI - it saves typing
Re: Regex check
by jethro (Monsignor) on Aug 15, 2008 at 01:34 UTC

    You might use //x to make your regex a bit more readable. Like this:

    '/ [\w().-]* \(? ([\w.-])? \)? \s* -> \s* $ /x'

    You could even use more than one line and comments to make the regex much clearer, but since we need to store it in a string I left that out

    I don't see anything which would produce an error but the regex part between the two parens (and the parens themselves) will never match anything because the first part already matched it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://704443]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2019-12-13 00:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found