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

Re^2: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)

by JDoolin (Novice)
on Mar 14, 2014 at 17:12 UTC ( [id://1078367]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)
in thread Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)

Hello, thank you for your prompt response. It appears that s///eeg gives me part of what I want. It seems to interpolate the $1 when it is alone. But it is an incomplete solution for me. If instead of just getting rid of the brackets I want to replace them with words:
#!/usr/bin/perl use warnings; use strict; sub replace { s/$_[0]/$_[1]/eeg; } $_ = 'a { b } c ( d ) e'; my $nobrackets = qr/[^\{^\}]+/; replace(qr/\{($nobrackets)\}/, ' leftbracket $1 rightbracket '); # I w +ant to replace the brackets with the words leftbracket and rightbrack +et. print "$_\n";
This appears to make the { b } vanish entirely.
  • Comment on Re^2: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)
  • Download Code

Replies are listed 'Best First'.
Re^3: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements)
by AnomalousMonk (Archbishop) on Mar 15, 2014 at 00:41 UTC

      Yes, I misunderstood his comment above.

      "2. In character classes (inside square brackets in a regex) you do not have to backslash brackets. ^ on the non-first position in a character class stands for itself."

      When I read 'you do not have to...' I understood that the backslashes are optional, but decided I liked to use them anyway. I completely missed his more important point here, that the repeated ^ actually negates the ^ character.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1078367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-18 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found