Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

regexp - how to embed a "/" within the executable part of the regexp when using /e

by princepawn (Parson)
on Sep 27, 2002 at 06:10 UTC ( [id://201125]=perlquestion: print w/replies, xml ) Need Help??

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

The following program:
$_ = '22C'; s/(\d+)C/($1*9/5)+32 . "F"/ge; print;
is a verbatim reproduction of slide 31of gnat's Stages of a Perl programmer. However the / in the executable part of the regexp is being misinterpreted.

How does one embed a slash in a regexp?

 
Edited: ~Fri Sep 27 15:00:32 2002 (GMT) by footpad: Removed incomplete <A HREF> tag, per Consideration.

Replies are listed 'Best First'.
Re: regexp - how to embed a "/" within the executable part of the regexp when using /e
by diotalevi (Canon) on Sep 27, 2002 at 06:15 UTC

    Either escape it as s/(\d+)C/($1*9\/5)+32 . "F"/ge; or change the delimiter so there isn't a conflict like s|(\d+)C|($1*9/5)+32 . "F"|ge;. perlre has more examples of this.

Re: regexp - how to embed a "/" within the executable part of the regexp when using /e
by Juerd (Abbot) on Sep 27, 2002 at 08:15 UTC

    s/(\d+)C/($1*9/5)+32 . "F"/ge;

    I like [] for matches, and {} for code. Maybe the Apocalypses inspired me...

    s[ (\d+) C ] { $1 * 9 / 5 + 32 . 'F' }gex;

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      I'm reconsidering my normal useage of both toothpicks and pipes. Of course - once you start writing your regular expressions more like a grammar then it starts to make sense to use balanced delimiters, stylisticly even. Of course grammars (using regular expressions, not Parse::RecDescent) are *still* fugly in perl5 but hey.

Re: regexp - how to embed a "/" within the executable part of the regexp when using /e
by Dedalus (Scribe) on Sep 27, 2002 at 10:57 UTC
    One simply escapes it, this works even if it is part of code to be executed:
    $_ = '22C'; s/(\d+)C/($1*9\/5)+32 . "F"/ge; print;
    produces 71.6F. Dedalus.
Re: regexp - how to embed a "/" within the executable part of the regexp when using /e
by trs80 (Priest) on Sep 29, 2002 at 05:35 UTC
    You could also do it without division.
    $_ = '22C'; s/(\d+)C/($1*9*.2)+32 . "F"/ge; print; print "\n"; $_ = '0C'; s/(\d+)C/($1*9*.2)+32 . "F"/ge; print; print "\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://201125]
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found