Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Limiting search on RegEx

by mr_p (Scribe)
on Jun 30, 2010 at 15:11 UTC ( #847345=perlquestion: print w/ replies, xml ) Need Help??
mr_p has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I trying to tell the difference if I have html or xml page. The best way I thought was to do a search on first 80 character and look for '<?xml', but I don't know how to limit characters on RegEx search. If anyone know Please let me know.

Thanks You.

Comment on Limiting search on RegEx
Re: Limiting search on RegEx
by Corion (Pope) on Jun 30, 2010 at 15:14 UTC

    Use substr on the left hand side of the regular expression.

Re: Limiting search on RegEx
by kennethk (Monsignor) on Jun 30, 2010 at 15:22 UTC
    As an alternative to Corion's suggestion, you can anchor your expression to the start of the string.

    /^.{0,80}\Q<?xml\E/s

    You will likely need the s modifier so . will match newlines and escaping expressions containing punctuation (\Q...\E) is a good habit to get into. See perlretut for details.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-27 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls