Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Regex in json: escaping forward slash

by mhearse (Hermit)
on Aug 22, 2012 at 21:07 UTC ( #989147=perlquestion: print w/ replies, xml ) Need Help??
mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I like using json for config files. It's easy on the eyes. But now I have a problem. I'm trying to figure out how to escape forward slashes (or anything that needs escaping). So that they are correctly interpreted by perl. Can someone help?
{ "regex" : { ".*/home/members/index.htm.*" : { "404" : { "reporting" : "1000", "paging" : "2000" } } } }

Comment on Regex in json: escaping forward slash
Download Code
Replies are listed 'Best First'.
Re: Regex in json: escaping forward slash
by Mr. Muskrat (Abbot) on Aug 22, 2012 at 21:23 UTC

    How about you show us the code you're using to read that JSON? Then we can tell you how to make it work properly.

      #!/usr/bin/env perl use strict; use JSON; use Data::Dumper; my $contents = `cat progconfig.json`; my $config = JSON->new->decode($contents); print Dumper($config);

        But what's the problem? Maybe you could provide an example of how your code is failing to deal with '/' properly. In the following example I decode your sample JSON, grab the key that looks like a regular expression, compile it as a regular expression, and use it to perform a match.

        use strict; use warnings; use JSON; use constant KEY => 0; use constant VALUE => 1; my $json_string = <<'END_JSON'; { "regex" : { ".*/home/members/index.htm.*" : { "404" : { "reporting" : "1000", "paging" : "2000" } } } } END_JSON my @test_strings = qw( web/home/members/index.htm?garbage=bye /home/memberzzzz/index.htm?garbage=bye ); my $decoded_json = JSON->new->decode( $json_string ); # The regex is contained within a hash key. my $regex_string = (%{$decoded_json->{regex}})[KEY]; my $regex = qr/$regex_string/; foreach my $test_string ( @test_strings ) { if( $test_string =~ m/$regex/ ) { print "Bingo! [$test_string] matches the pattern '$regex_string'\n +"; } else { print "Boo! [$test_string] doesn't match the pattern '$regex_strin +g'\n"; } }

        No special escaping of '/' here. The qr// operator is convenient.


        Dave

        That code doesn't exhibit the problem you are trying to solve. (It isn't trying to do anything that requires escaping the slashes.)

        Output:

        $VAR1 = { 'regex' => { '.*/home/members/index.htm.*' => { '404' => { ' +reporting' => '1000', ' +paging' => '2000' } } } };

        Even when I use that key as a regex, it works for me.

        #!/usr/bin/env perl use strict; use warnings; use JSON; use Data::Dumper; my $contents = `cat progconfig.json`; my $config = JSON->new->decode($contents); my $re = (keys %{$config->{regex}})[0]; print "re: $re\n"; my $path = '/some/fake/path/home/members/index.html'; if ( $path =~ /$re/ ) { print "$path matched regex\n"; }

        Output:

        re: .*/home/members/index.htm.* /some/fake/path/home/members/index.html matched regex

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2015-07-31 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls