Friday, April 29, 2016

RegularExpression

URL 분석

정규표현식(기본적인 표현식)
^((http[s]?|ftp):\/\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*?)?(#[\w\-]+)?$

정규표현식(/? 처럼 인덱스 생략까지 표현)
^((http[s]?|ftp):\/\/)?\/?([^:\/\s]+)((\/[\w\-\.]+)*\/)([\w\-\.]+[^#\?\s]+|[\w\-\.]?[^#\?\s]?)(.*?)?(#[\w\-]+)?$

#! /bin/env perl

$uri = "http://example.com/index.php?title=example-release&redirect=no#Packages";

$uri =~ /^((http[s]?|ftp):\/\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*?)?(#[\w\-]+)?$/;

print "PROTOCOL: $2\n";
print "HOST: $3\n";
print "PATH: $4\n";
print "FILE: $6\n";
print "QUERY: $7\n";
print "HASH: $8\n";

$ perl example.pl

PROTOCOL: http
HOST: example.com
PATH: /
FILE: index.php
QUERY: ?title=example-release&redirect=no
HASH: #Packages


Post a Comment