Skip to content

Commit

Permalink
正则
Browse files Browse the repository at this point in the history
  • Loading branch information
rry committed Mar 14, 2016
1 parent a3e7f1f commit 9d0cd63
Show file tree
Hide file tree
Showing 222 changed files with 432 additions and 0 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/1-angry-professor.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/10-the-grid-search.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/11-cavity-map.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/11-cavity-map.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/12-library-fine.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/12-library-fine.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/12-manasa-and-stones.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/12-manasa-and-stones.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/13-acm-icpc-team.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/13-acm-icpc-team.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/13-acm-icpc-team.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/14-extra-long-factorials.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/14-extra-long-factorials.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/15-taum-and-bday.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/15-taum-and-bday.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/16-the-time-in-words.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/17-kaprekar-numbers.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/18-encryption.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/18-encryption.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/19-matrix-rotation-algo.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/2-sherlock-and-the-beast.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/2-sherlock-and-the-beast.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/3-utopian-tree.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/3-utopian-tree.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/4-find-digits.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/4-find-digits.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/5-sherlock-and-squares.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/5-sherlock-and-squares.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/6-service-lane.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/6-service-lane.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/7-cut-the-sticks.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/7-cut-the-sticks.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/8-chocolate-feast.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/9-caesar-cipher-1.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/9-caesar-cipher-1.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/1-tutorial-intro.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/1-tutorial-intro.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/10-quicksort4.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/11-countingsort1.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/12-countingsort2.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/13-countingsort3.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/14-countingsort4.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/14-countingsort4.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/15-closest-numbers.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/16-find-median.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/17-sherlock-and-watson.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/18-almost-sorted.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/18-almost-sorted.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/19-sherlock-and-pairs.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/2-insertionsort1.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/20-insertion-sort.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/20-insertion-sort.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/3-insertionsort2.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/4-correctness-invariant.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/5-runningtime.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/6-quicksort1.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/7-quicksort2.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/8-quicksort3.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/8-quicksort3.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/9-bigger-is-greater.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Implementation/Sorting/9-bigger-is-greater.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/1-sherlock-and-array.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/1-sherlock-and-array.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/2-icecream-parlor.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/3-maximise-sum.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/3-maximise-sum.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/4-missing-numbers.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/4-missing-numbers.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Search/5-connected-cell-in-a-grid.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/1-pangrams.c
100644 → 100755
Empty file.
Empty file modified Algorithm/String/1-pangrams.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/10-palindrome-index.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/11-reverse-shuffle-merge.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/12-the-love-letter-mystery.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/12-the-love-letter-mystery.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/13-common-child.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/14-morgan-and-a-string.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/15-sherlock-and-valid-string.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/16-count-string.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/17-string-function-calculation.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/17-string-function-calculation.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/2-funny-string.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/2-funny-string.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/22-string-similarity.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/22-string-similarity.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/3-alternating-characters.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/3-alternating-characters.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/4-game-of-thrones.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/4-game-of-thrones.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/5-gem-stones.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/5-gem-stones.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/6-make-it-anagram.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/6-make-it-anagram.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/7-anagram.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/7-anagram.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/8-two-string.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/String/8-two-string.php
100644 → 100755
Empty file.
Empty file modified Algorithm/String/9-sherlock-and-anagrams.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/1-solve-me-first.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/2-simple-array-sum.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/3-a-very-big-sum.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/4-diagonal-difference.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/4-diagonal-difference.php
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/5-plus-minus.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/6-staircase.c
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/7-time-conversion.cpp
100644 → 100755
Empty file.
Empty file modified Algorithm/Warmup/7-time-conversion.php
100644 → 100755
Empty file.
Empty file modified C++/Classes/1-c-tutorial-struct.cpp
100644 → 100755
Empty file.
Empty file modified C++/Classes/2-c-tutorial-class.cpp
100644 → 100755
Empty file.
Empty file modified C++/Classes/3-classes-objects.cpp
100644 → 100755
Empty file.
Empty file modified C++/Classes/4-c-class-templates.cpp
100644 → 100755
Empty file.
Empty file modified C++/Classes/5-box-it.cpp
100644 → 100755
Empty file.
Empty file modified C++/Classes/6-attending-workshops.cpp
100644 → 100755
Empty file.
Empty file modified C++/Classes/7-abstract-classes-polymorphism.cpp
100644 → 100755
Empty file.
Empty file modified C++/Inheritance/1-inheritance-introduction.cpp
100644 → 100755
Empty file.
Empty file modified C++/Inheritance/2-rectangle-area.cpp
100644 → 100755
Empty file.
Empty file modified C++/Inheritance/3-multi-level-inheritance-cpp.cpp
100644 → 100755
Empty file.
Empty file modified C++/Inheritance/4-accessing-inherited-functions.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/1-cpp-hello-world.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/10-variable-sized-arrays.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/11-overload-operators.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/12-virtual-functions.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/13-bitset-1.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/2-cpp-input-and-output.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/3-c-tutorial-basic-data-types.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/4-c-tutorial-conditional-if-else.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/5-c-tutorial-for-loop.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/6-c-tutorial-functions.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/7-c-tutorial-pointer.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/8-arrays-introduction.cpp
100644 → 100755
Empty file.
Empty file modified C++/Introdution/9-operator-overloading.cpp
100644 → 100755
Empty file.
Empty file modified C++/STL/1-vector-sort.cpp
100644 → 100755
Empty file.
Empty file modified C++/STL/2-vector-erase.cpp
100644 → 100755
Empty file.
Empty file modified C++/STL/3-cpp-lower-bound.cpp
100644 → 100755
Empty file.
Empty file modified C++/STL/4-cpp-sets.cpp
100644 → 100755
Empty file.
Empty file modified C++/STL/5-cpp-maps.cpp
100644 → 100755
Empty file.
Empty file modified C++/STL/6-deque-stl.cpp
100644 → 100755
Empty file.
Empty file modified C++/Strings/1-c-tutorial-strings.cpp
100644 → 100755
Empty file.
Empty file modified C++/Strings/2-c-tutorial-stringstream.cpp
100644 → 100755
Empty file.
Empty file modified C++/Strings/3-attribute-parser.php
100644 → 100755
Empty file.
Empty file modified DataStructure/Arrays/1-arrays-ds.cpp
100644 → 100755
Empty file.
Empty file modified DataStructure/Arrays/2-2d-array.cpp
100644 → 100755
Empty file.
Empty file modified DataStructure/Arrays/3-dynamic-array.cpp
100644 → 100755
Empty file.
Empty file modified DataStructure/Arrays/3-dynamic-array.php
100644 → 100755
Empty file.
Empty file modified DataStructure/Arrays/4-sparse-arrays.php
100644 → 100755
Empty file.
Empty file modified DataStructure/CMakeLists.txt
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified DataStructure/LinkedLists/7-reverse-a-linked-list.cpp
100644 → 100755
Empty file.
Empty file modified DataStructure/LinkedLists/8-compare-two-linked-lists.cpp
100644 → 100755
Empty file.
Empty file modified DataStructure/LinkedLists/9-merge-two-sorted-linked-lists.cpp
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions Regex/Applications/1-detect-html-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function processData(input) {
var regex=/<a.*?href="(.*?)".*?>(.*?)<\/a>/ig;
var output=[];
input.replace(regex,function(_,href,text){
output.push(href.trim()+','+text.replace(/<.*?>/g,'').trim())
});
console.log(output.join('\n'));
}
15 changes: 15 additions & 0 deletions Regex/Applications/1-detect-html-links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$html = '';
while ($t --) {
$html .= trim(fgets($handle));
}

if (preg_match_all('/<a .*?href="(.*?)".*?>(.*?)<\/a>/', $html, $matches)) {
foreach ($matches[1] as $i => $link) {
printf("%s,%s\n", $link, trim(strip_tags($matches[2][$i])));
}
}
21 changes: 21 additions & 0 deletions Regex/Applications/10-detecting-valid-latitude-and-longitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$lines = [];
while ($t --) {
$lines[] = trim(fgets($handle));
}

foreach ($lines as $line) {
if (preg_match_all('/\(\+?-?((?:[1-9]\d*)|(?:[1-9]\d*)\.\d+), \+?-?((?:[1-9]\d*)|(?:[1-9]\d*)\.\d+)\)/', $line, $matches)) {
if ($matches[1][0] > 90 || $matches[2][0] > 180) {
echo "Invalid\n";
} else {
echo "Valid\n";
}
} else {
echo "Invalid\n";
}
}
11 changes: 11 additions & 0 deletions Regex/Applications/11-hackerrank-tweets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$twitters = '';
while ($t --) {
$twitters .= trim(fgets($handle)) . " ";
}

echo preg_match_all('/HackerRank/i', $twitters);
15 changes: 15 additions & 0 deletions Regex/Applications/12-stack-exchange-scraper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$handle = fopen("php://stdin", "r");

$html = '';
$t = 3600;
while ($t --) {
$html .= trim(fgets($handle)) . "\n";
}

preg_match_all('/(?:<h3><a href="\/questions\/(\d*).*>(.*)<\/a>|class="relativetime">(.*)<\/span>)/', $html, $matches);

for ($i = 0; $i < count($matches[0]) / 2; $i ++) {
printf("%s;%s;%s\n", $matches[1][$i * 2], $matches[2][$i * 2], $matches[3][($i + 1) * 2 - 1]);
}
11 changes: 11 additions & 0 deletions Regex/Applications/13-utopian-identification-number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$regex = '/^[a-z]{0,3}\d{2,8}[A-Z]{3,}$/';
while ($t --) {
fscanf($handle, "%s", $s);

echo preg_match($regex, $s) ? "VALID\n" : "INVALID\n";
}
11 changes: 11 additions & 0 deletions Regex/Applications/14-valid-pan-format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$regex = '/^[A-Z]{5}[0-9]{4}[A-Z]$/';
while ($t --) {
fscanf($handle, "%s", $s);

echo preg_match($regex, $s) ? "VALID\n" : "INVALID\n";
}
23 changes: 23 additions & 0 deletions Regex/Applications/15-find-hackerrank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

while ($t --) {
$s = trim(fgets($handle));

if ($s == 'hackerrank') {
echo "0\n";
} else {
$pos = strpos($s, 'hackerrank');
if ($pos === false) {
echo "-1\n";
} else if ($pos === 0) {
echo "1\n";
} else if ($pos + 10 == strlen($s)) {
echo "2\n";
} else {
echo "-1\n";
}
}
}
20 changes: 20 additions & 0 deletions Regex/Applications/16-saying-hi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);
$languages = ['C','CPP','JAVA','PYTHON','PERL','PHP','RUBY','CSHARP','HASKELL','CLOJURE','BASH','SCALA','ERLANG','CLISP','LUA','BRAINFUCK','JAVASCRIPT','GO','D','OCAML','R','PASCAL','SBCL','DART','GROOVY','OBJECTIVEC'];

while ($t --) {
$s = trim(fgets($handle));

if (preg_match('/(\d{1,5}) ([A-Z]+)/', $s, $matches)) {
print_r($matches);
if ($matches[1] >= 10000 && $matches[1] < 100000 && in_array($matches[2], $languages)) {
echo "VALID\n";
} else {
echo "INVALID\n";
}
} else {
echo "INVALID\n";
}
}
20 changes: 20 additions & 0 deletions Regex/Applications/17-programming-language-detection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$handle = fopen("php://stdin", "r");

$code = '';
$t = 1000;
while ($t --) {
$code .= trim(fgets($handle)) . "\n";
}

$regex_c = '/#include/';
$regex_java = '/import java/';

if (preg_match($regex_c, $code)) {
echo "C\n";
} else if (preg_match($regex_java, $code)) {
echo "Java\n";
} else {
echo "Python\n";
}
13 changes: 13 additions & 0 deletions Regex/Applications/18-split-number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);
$regex = '/^(\d{1,3})(\?|(?: )|(?:-))(\d{1,3})\2(\d{4,10})$/';

while ($t --) {
$string = trim(fgets($handle));

if (preg_match($regex, $string, $matches)) {
printf("CountryCode=%s,LocalAreaCode=%s,Number=%s\n", $matches[1], $matches[3], $matches[4]);
}
}
13 changes: 13 additions & 0 deletions Regex/Applications/19-html-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);
$regex = '//';

while ($t --) {
$string = trim(fgets($handle));

if (preg_match($regex, $string, $matches)) {
printf("CountryCode=%s,LocalAreaCode=%s,Number=%s\n", $matches[1], $matches[3], $matches[4]);
}
}
23 changes: 23 additions & 0 deletions Regex/Applications/2-detect-html-tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$html = '';
while ($t --) {
$html .= trim(fgets($handle));
}

$tags = [];
$regex = '/(<\s*([\w]+) ?.*?>|\[.*?\])/';

if (preg_match_all($regex, $html, $matches)) {
foreach ($matches[2] as $g) {
$tags[] = $g ?: 'a';
}
}

$tags = array_unique($tags);
sort($tags);

echo implode(";", $tags) . PHP_EOL;
46 changes: 46 additions & 0 deletions Regex/Applications/20-html-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);
$regex_1 = '/<([a-zA-Z0-9]+) ?(?:\w*="?\'?[^"\']*\'?"? ?){0,}/';
$regex_2 = '/ (\w*)=/';

$code = [];

while ($t --) {
$code[] = trim(fgets($handle));
}

$tags = [];

foreach ($code as $c) {
if (preg_match_all($regex_1, $c, $matches_1)) {
for ($i = 0; $i < count($matches_1[0]); $i ++) {
if (strpos($matches_1[0][$i], '=') !== false) {
preg_match_all($regex_2, $matches_1[0][$i], $matches_2);

foreach ($matches_2[1] as $attr) {
if (! isset($tags[$matches_1[1][$i]])) {
$tags[$matches_1[1][$i]][] = $attr;
} else {
if (! in_array($attr, $tags[$matches_1[1][$i]])) {
$tags[$matches_1[1][$i]][] = $attr;
}
}
}
} else {
if (! isset($tags[$matches_1[1][$i]])) {
$tags[$matches_1[1][$i]] = [];
}
}
}
}
}

ksort($tags);

foreach ($tags as $tag => $attributes) {
sort($attributes);

printf("%s:%s\n", $tag, implode(',', $attributes));
}
18 changes: 18 additions & 0 deletions Regex/Applications/21-uk-and-us.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$words = '';
while ($t --) {
$words .= trim(fgets($handle)) . " ";
}

fscanf($handle, "%d", $n);

while ($n --) {
fscanf($handle, "%s", $s);
$regex = '/' . substr($s, 0, strlen($s) - 2) . '[sz]' . substr($s, strlen($s) - 1) . '/';

printf("%d\n", preg_match_all($regex, $words));
}
23 changes: 23 additions & 0 deletions Regex/Applications/22-uk-and-us-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$words = '';
while ($t --) {
$words .= trim(fgets($handle)) . " ";
}

fscanf($handle, "%d", $n);

while ($n --) {
fscanf($handle, "%s", $s);
$s = str_replace("our", "ou?r", $s);
$regex = '/(?=\b' . $s . '\b)/i';

if (preg_match_all($regex, $words, $matches)) {
echo count($matches[0]) . "\n";
} else {
echo "0\n";
}
}
25 changes: 25 additions & 0 deletions Regex/Applications/3-find-substring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

while ($t --) {
$lines[] = trim(fgets($handle));
}

fscanf($handle, "%d", $n);

while ($n --) {
$count = 0;
fscanf($handle, "%s", $pattern);
$regex = "/[0-9a-zA-Z_]+" . $pattern . "[0-9a-zA-Z_]+/";
foreach ($lines as $line) {
foreach (explode(" ", $line) as $word) {
if (preg_match($regex, $word)) {
$count ++;
}
}
}

echo $count . PHP_EOL;
}
12 changes: 12 additions & 0 deletions Regex/Applications/4-alien-username.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

while ($t --) {
fscanf($handle, "%s", $username);

$regex = '/^[\._]\d+[a-zA-Z]*_?$/';

echo preg_match($regex, $username) ? "VALID\n" : "INVALID\n";
}
19 changes: 19 additions & 0 deletions Regex/Applications/5-ip-address-validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$ip4_regex = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
$ip6_regex = '/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/';

while ($t --) {
fscanf($handle, "%s", $str);

if (preg_match($ip4_regex, $str)) {
printf("IPv4\n");
} else if (preg_match($ip6_regex, $str)) {
printf("IPv6\n");
} else {
printf("Neither\n");
}
}
20 changes: 20 additions & 0 deletions Regex/Applications/6-find-a-word.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$lines = '';
while ($t --) {
$lines .= ' ' . trim(fgets($handle));
}

fscanf($handle, "%d", $n);

while ($n --) {
fscanf($handle, "%s", $word);
$count = 0;

$regex = '/(\b' . $word. '\b)/';

printf("%d\n", preg_match_all($regex, $lines));
}
Loading

0 comments on commit 9d0cd63

Please sign in to comment.