Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How can I match this }\n} in perl

by nkg (Initiate)
on Feb 20, 2019 at 10:01 UTC ( [id://1230231]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks,

i need to match "}\n}" (without quotes and newline as in real).description of matching characters is below. How can I do it?

}

}

Replies are listed 'Best First'.
Re: How can I match this }\n} in perl
by hippo (Bishop) on Feb 20, 2019 at 10:23 UTC

    One approach (of many):

    use strict; use warnings; use Test::More tests => 1; my $have = "foo}\n}"; my $re = qr/}\n}/; like $have, $re, 'Matched';

    Update: reduced the regex to make it even simpler.

Re: How can I match this }\n} in perl
by haukex (Archbishop) on Feb 21, 2019 at 08:15 UTC
    i need to match "}\n}" (without quotes and newline as in real).

    The regex to match "}\n}" is /}\n}/ (unless you're using m{}, in which case you need to write m{\}\n\}}). Since that's pretty obvious, I wonder if maybe you're having problems with some of your code? If that's the case, maybe you could show the code you're having problems with in the form of a Short, Self-Contained, Correct Example: a short, runnable piece of code that reproduces the issue, short sample input, the expected output for that input, and the output you're getting instead, including any error messages (code and input/output in <code> tags).

Re: How can I match this }\n} in perl
by chenhonkhonk (Acolyte) on Feb 22, 2019 at 18:54 UTC

    Without more information, that regex would work. Is it perhaps there's whitespace you're not accounting for, since it looks like you're searching for braces that end a block?

    If that's the case, I think you would want /}\n\w*}/ There's also a possibility you're editing a document from another filesystem, and newline is defaulting to your system. /}\n\r?}/ catches both Linux-y/Windows line endings.

      whitespace you're not accounting for ... If that's the case, I think you would want /}\n\w*}/

      That should probably be /}\n\s*}/, since \w means a word character.

      /}\n\r?}/

      That should probably be /}\r?\n}/, since \n\r would be LFCR (see also).

        Thanks. I don't use regex too often anymore but I mostly remember it!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found