正规表现勉强会
- 11. 色々あります。
POSIX -> 標準規格
PCRE -> Perl
鬼車-> Ruby
Re2 -> Google
GNU Regex -> Unix
- 13. /)
///)
/,.=?''"/
/ i f ,.r='"-‐'つ____ こまけぇこたぁいいんだよ!!
/ / _,.-‐'~/⌒ ⌒\
/ ,i ,二??( ●). (●)\
/ ?il?フ::::::⌒(__人__)⌒::::: \
,????,!,!| |r┬-| |
/ i?ヾヽ_/?"\ `ー'? /
- 19. .
*
+
?
改行を除くすべての文字
0回以上の繰り返し
1回以上の繰り返し
0または1回or最小マッチ
- 21. [ ]
{ }
( )
|
文字の集合
量指定子
グループ
または
- 22. [a-z] aからz
[a-zA-Z] aからz + AからZ
[0-9] 0から9
[ac] aとc
[^ac] aとcを含まない
(abc) abc文字列
(ab|cd) abもしくはcd
- 25. /i
/m
/u
オプション
大文字小文字区別しない
.が改行にもマッチ
文字コードUTF-8
- 26. /abc/i =~ "ABC"
=> 0
"abnc".scan(/.+/)
=> ["ab", "c"]
"abnc".scan(/.+/m)
=> ["abnc"]
- 37. function validate_email($email, $strict = true) {
$dot_string = $strict ?
'(?:[A-Za-z0-9!#$%&*+=?^_`{|}~'/-]|(?<!.|A).(?!.|@))' :
'(?:[A-Za-z0-9!#$%&*+=?^_`{|}~'/.-])'
;
$quoted_string = '(?:|"|?[A-Za-z0-9!#$%&*+=?^_`{|}~()<>[]:;@,. '/-])';
$ipv4_part = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
$ipv6_part = '(?:[A-fa-f0-9]{1,4})';
$fqdn_part = '(?:[A-Za-z](?:[A-Za-z0-9-]{0,61}?[A-Za-z0-9])?)';
$ipv4 = "(?:(?:{$ipv4_part}.){3}{$ipv4_part})";
$ipv6 = '(?:' .
"(?:(?:{$ipv6_part}:){7}(?:{$ipv6_part}|:))" . '|' .
"(?:(?:{$ipv6_part}:){6}(?::{$ipv6_part}|:{$ipv4}|:))" . '|' .
"(?:(?:{$ipv6_part}:){5}(?:(?::{$ipv6_part}){1,2}|:{$ipv4}|:))" . '|' .
"(?:(?:{$ipv6_part}:){4}(?:(?::{$ipv6_part}){1,3}|(?::{$ipv6_part})?:{$ipv4}|:))" . '|' .
"(?:(?:{$ipv6_part}:){3}(?:(?::{$ipv6_part}){1,4}|(?::{$ipv6_part}){0,2}:{$ipv4}|:))" . '|' .
"(?:(?:{$ipv6_part}:){2}(?:(?::{$ipv6_part}){1,5}|(?::{$ipv6_part}){0,3}:{$ipv4}|:))" . '|' .
"(?:(?:{$ipv6_part}:){1}(?:(?::{$ipv6_part}){1,6}|(?::{$ipv6_part}){0,4}:{$ipv4}|:))" . '|' .
"(?::(?:(?::{$ipv6_part}){1,7}|(?::{$ipv6_part}){0,5}:{$ipv4}|:))" .
')';
$fqdn = "(?:(?:{$fqdn_part}.)+?{$fqdn_part})";
$local = "({$dot_string}++|("){$quoted_string}++")";
$domain = "({$fqdn}|[{$ipv4}]|[{$ipv6}]|[{$fqdn}])";
$pattern = "/A{$local}@{$domain}z/";
return preg_match($pattern, $email, $matches) &&
(
!empty($matches[2]) && !isset($matches[1][66]) && !isset($matches[0][256]) ||
!isset($matches[1][64]) && !isset($matches[0][254])
)
;
}
参照:http://qiita.com/mpyw/items/257eabe0b43b1e02e6f7
- 38. ____
/:::::::::: u\
/:::::::::⌒ 三. ⌒\
/:::::::::: ( ○)三(○)\
|::::::::::::::::⌒(__人__)⌒ | _________
\:::::::::: ` ⌒? ,/. | | |
ノ::::::::::u \ | | |
. /:::::::::::::::: u | | |
|::::::::::::: l u | | |
ヽ::::::: -一ー_~?⌒)^),-?|_|_______|
ヽ::::::::___,ノγ⌒ヽ)二二二| | |
- 39. RFC5321/5322
Abc@example.com
Abc.123@example.com
user+mailbox/department=shipping@example.com
!#$%&'*+-/=?^_`.{|}~@example.com
"Abc@def"@example.com
"Fred Bloggs"@example.com
"Joe.Blow"@example.com
参照:http://ja.wikipedia.org/wiki/メールアドレス
- 43. HTTP URI
"http://hoge.com/piyo"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
| ?" ?)(__人__)????___________
\ ゝ'?≦ 三?| | |
__/ ?≧ 三= | | |
| | / , -?, ≧= .| | |
| | / / .??,?> | | |
| | | ⌒ ーnnn ,≦`Vヾヾ≧ |_|___________|
 ̄\__?("二)??/?????\?l二二l二二_|_|__|_
- 48. for (1..100) {
s/(?<=[05])$/Buzz/;
s/^([0369]|[147]([0369]|[147][0369]*[258])*
([258]|[147][0369]*[147])|
[258]([0369]|[258][0369]*[147])*
([147]|[258][0369]*[258]))+Buzz$/FizzBuzz/;
s/^([0369]|[147]([0369]|[147][0369]*[258])*
([258]|[147][0369]*[147])|
[258]([0369]|[258][0369]*[147])*
([147]|[258][0369]*[258]))+$/Fizz/;
s/d+(?=D)//;
print "$_n";
}
- 49. for (1..100) {
s/(?<=[05])$/Buzz/;
s/^([0369]|[147]([0369]|[147][0369]*[258])*
([258]|[147][0369]*[147])|
[258]([0369]|[258][0369]*[147])*
([147]|[258][0369]*[258]))+Buzz$/FizzBuzz/;
s/^([0369]|[147]([0369]|[147][0369]*[258])*
([258]|[147][0369]*[147])|
[258]([0369]|[258][0369]*[147])*
([147]|[258][0369]*[258]))+$/Fizz/;
s/d+(?=D)//;
print "$_n";
}
5の倍数の時、Buzzを追加
Buzzがあって、
かつ3の倍数の時残ったFizzBuzz 3の倍数をFizzに
最後に必要ない数値を削除