Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^3: Regex in json: escaping forward slash by davido
in thread Regex in json: escaping forward slash by mhearse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found