2016-08-22 3 views
-1
$text = <<"EOF"; 
{"product_id": "5I20SL43MWDD", "title": "Woodland Imports 2 Piece Ceramic Vase Set", "primary_shelf": [{"all_paths_str": "[[\"Home Page\",\"Home\",\"Decor\",\"Vases\"]]", "locale": "en_US", "value": "Vases", "path_id": "[\"0\",\"4044\",\"133012\",\"1043827\"]", "all_paths_id": "[[\"0\",\"4044\",\"133012\",\"1043827\"]]", "tag_source": "rule", "id": "1043827", "path_str": "[\"Home Page\",\"Home\",\"Decor\",\"Vases\"]"}], "alternate_shelves": "1043827", "last_updated_at": 1471417348, "primary_category_path": "0:4044:133012:1043827", "short_description": "Includes two vase. Gray and cream color palette. No assembly required. 5 in. L x 5 in. W x 12 in. H. Weight: 6 lbs.. Check out this Vase that will beautify your interiors. Can be kept assorted or separately depending upon your needs. For added beauty, you can also add flowers to this ceramic vase to flourish your interiors with fragrance. This vase will steal the spotlight and make you win appreciations from many. Besides, this vase can also be gifted to your beloved ones.", "item_id": "40966792", "attributes": {"package_quantity": ["1"], "assembled_product_width_raw_data": ["6.0"], "assembled_product_length_raw_data": ["6.0"], "is_always_show": ["N"], "assembled_product_height_raw_data": ["15.0", "15.0"], "color": ["Brown", "Brown"], "small_parts_warning_code": ["0"], "manufacturer_part_number": ["78681"], "primary_shelf_id": ["1043827"], "shape": ["Bud"], "manufacturer_suggested_retail_price": ["75.84"], "country_of_origin_components": ["Imported"], "assembled_product_length": ["6.0 IN", "6.0"], "category": ["Vases", "Vases"], "has_warranty": ["N"], "character_primary_category_path": ["Home/Decor/Vases"], "item_master_created_date": ["1414926519000"], "offer_lifecycle_status": ["ACTIVE"], "new": ["N"], "size": ["12\" H x 5\" W x 5\" D", "12\" H x 5\" W x 5\" D"], "type": ["Other Vases"], "brand": ["Woodland Imports", "Woodland Imports"], "all_shelves": ["Vases"], "is_preorder": ["N"], "primary_shelf": ["Vases"], "material": ["Ceramic", "Ceramic"], "assembled_product_width": ["6.0 IN", "6.0"], "california_residents_prop_65_warning_required": ["N"], "lifestage": ["Adult", "Adult"], "show_button_in": ["3"], "warranty_information": ["Warranty not available for this item"], "char_primary_category_path": ["Home/Decor/Vases"], "manufacturer": ["Woodland Imports"], "actual_color": ["Brown", "Brown"], "data_source": ["Catalog"], "product_type": ["Vases"], "size_raw_data": ["12"], "wupc": ["0083730378681"], "display_status": ["PUBLISHED"], "assembled_product_weight": ["7 Pounds"], "country_of_origin_assembly": ["Imported"], "assembled_product_height": ["15.0 IN", "15.0"], "warranty_length": ["Warranty not available for this item"], "model": ["78681"], "price_per_unit_quantity": ["1"]}, "long_description": "WLI15311<br /><strong>Features</strong><br /><li>Grey and cream color palette</li><li>Made of quality ceramic</li><li>Product Type: Floor vase</li><li>Shape: Bud</li><li>Color: Grey and cream</li><li>Primary Material: Ceramic</li> <br /><strong>Dimensions</strong><br />Size 15'' H x 6'' W x 6'' D<br /><li>Overall Height - Top to Bottom: 15''</li><li>Overall Width - Side to Side: 6''</li><li>Overall Depth - Front to Back: 6''</li><li>Overall Product Weight: 5.93 lbs</li><br />Size 12'' H x 5'' W x 5'' D<br /><li>Overall Height - Top to Bottom: 12''</li><li>Overall Width - Side to Side: 5''</li><li>Overall Depth - Front to Back: 5''</li><li>Overall Product Weight: 3.24 lbs</li>"} 
EOF 

이것은 json 형식의 텍스트입니다. 모든 것이 제대로오고 있지만, long_description 정규식 작동하지 않는 이유를 모르겠어요Perl long_description에 대한 정규 표현식이 작동하지 않습니다.

my ($title)=$text=~/"title"\s*:\s*"(.*?)"\s*,\s*"/is;** 
my ($long_des)=$text=~/"long_description"\s*:\s*"(.*?)"\s*,\s*"/is; 

, 나는 빈을 얻을 : 나는이 정규식을 사용하고 같은

short_description로하고 long_description 특정 필드를 추출합니다 끈. 이 문제를 해결하려면 무엇을해야합니까?

+5

, 왜'JSON'를 사용하여 자신에게 번거 로움을 저장합니다

다행히, 펄은 간단하게 JSON 모듈이? –

+0

나는 왜이 사람들이 질문을 downvote하는지 모른다. 내가 그 질문을하는 것보다 더 잘 알지 못한다. Thnx는 대답하고 나를 더 나은 방법으로 보여주었습니다. –

답변

5

나는 한 걸음 뒤로 물러나라고 제안 할 것이다. JSON의 상황에 따라 JSON을 정규식으로 해킹하는 것은 불쾌합니다. 정규 표현식은 상황에 맞는 것이 아니므로 항상 해킹 된 해결책입니다. 당신이 JSON을 구문 분석하는 경우

#!/usr/bin/env perl 

use strict; 
use warnings; 
use Data::Dumper; 
use JSON; 

my $json_str = q({"product_id": "5I20SL43MWDD", "title": "Woodland Imports 2 Piece Ceramic Vase Set", "primary_shelf": [{"all_paths_str": "[[\"Home Page\",\"Home\",\"Decor\",\"Vases\"]]", "locale": "en_US", "value": "Vases", "path_id": "[\"0\",\"4044\",\"133012\",\"1043827\"]", "all_paths_id": "[[\"0\",\"4044\",\"133012\",\"1043827\"]]", "tag_source": "rule", "id": "1043827", "path_str": "[\"Home Page\",\"Home\",\"Decor\",\"Vases\"]"}], "alternate_shelves": "1043827", "last_updated_at": 1471417348, "primary_category_path": "0:4044:133012:1043827", "short_description": "Includes two vase. Gray and cream color palette. No assembly required. 5 in. L x 5 in. W x 12 in. H. Weight: 6 lbs.. Check out this Vase that will beautify your interiors. Can be kept assorted or separately depending upon your needs. For added beauty, you can also add flowers to this ceramic vase to flourish your interiors with fragrance. This vase will steal the spotlight and make you win appreciations from many. Besides, this vase can also be gifted to your beloved ones.", "item_id": "40966792", "attributes": {"package_quantity": ["1"], "assembled_product_width_raw_data": ["6.0"], "assembled_product_length_raw_data": ["6.0"], "is_always_show": ["N"], "assembled_product_height_raw_data": ["15.0", "15.0"], "color": ["Brown", "Brown"], "small_parts_warning_code": ["0"], "manufacturer_part_number": ["78681"], "primary_shelf_id": ["1043827"], "shape": ["Bud"], "manufacturer_suggested_retail_price": ["75.84"], "country_of_origin_components": ["Imported"], "assembled_product_length": ["6.0 IN", "6.0"], "category": ["Vases", "Vases"], "has_warranty": ["N"], "character_primary_category_path": ["Home/Decor/Vases"], "item_master_created_date": ["1414926519000"], "offer_lifecycle_status": ["ACTIVE"], "new": ["N"], "size": ["12\" H x 5\" W x 5\" D", "12\" H x 5\" W x 5\" D"], "type": ["Other Vases"], "brand": ["Woodland Imports", "Woodland Imports"], "all_shelves": ["Vases"], "is_preorder": ["N"], "primary_shelf": ["Vases"], "material": ["Ceramic", "Ceramic"], "assembled_product_width": ["6.0 IN", "6.0"], "california_residents_prop_65_warning_required": ["N"], "lifestage": ["Adult", "Adult"], "show_button_in": ["3"], "warranty_information": ["Warranty not available for this item"], "char_primary_category_path": ["Home/Decor/Vases"], "manufacturer": ["Woodland Imports"], "actual_color": ["Brown", "Brown"], "data_source": ["Catalog"], "product_type": ["Vases"], "size_raw_data": ["12"], "wupc": ["0083730378681"], "display_status": ["PUBLISHED"], "assembled_product_weight": ["7 Pounds"], "country_of_origin_assembly": ["Imported"], "assembled_product_height": ["15.0 IN", "15.0"], "warranty_length": ["Warranty not available for this item"], "model": ["78681"], "price_per_unit_quantity": ["1"]}, "long_description": "WLI15311<br /><strong>Features</strong><br /><li>Grey and cream color palette</li><li>Made of quality ceramic</li><li>Product Type: Floor vase</li><li>Shape: Bud</li><li>Color: Grey and cream</li><li>Primary Material: Ceramic</li> <br /><strong>Dimensions</strong><br />Size 15'' H x 6'' W x 6'' D<br /><li>Overall Height - Top to Bottom: 15''</li><li>Overall Width - Side to Side: 6''</li><li>Overall Depth - Front to Back: 6''</li><li>Overall Product Weight: 5.93 lbs</li><br />Size 12'' H x 5'' W x 5'' D<br /><li>Overall Height - Top to Bottom: 12''</li><li>Overall Width - Side to Side: 5''</li><li>Overall Depth - Front to Back: 5''</li><li>Overall Product Weight: 3.24 lbs</li>"}); 
my $json = from_json($json_str); 

print Dumper $json; 

print "LONG:", $json ->{long_description},"\n"; 
+0

친구 나는 이것을 다음과 같이 사용하고 싶다. my $ json_str = q ($ text); 그것은 작동하지 않습니다. 귀하의 솔루션을 사용하고 있습니다 –

+0

그렇지 않습니다. 'q()'는 사실상 작은 따옴표입니다 (문자열의 따옴표로 인해 사용합니다). 따라서 문자 그대로 '$ text'를 구문 분석하려고합니다. 이미 변수에있는 경우'my $ json = from_json ($ text);을 사용하십시오. – Sobrique

관련 문제