Snippets

sironekotoro 正規表現を使わずに文字のパターンマッチ non_use_regex.pl

You are viewing an old version of this snippet. View the current version.
Revised by sironekotoro de91c99
use strict;
use warnings;

# 第4回 サブルーチン/正規表現編
# https://github.com/perl-entrance-org/workshop-2018/blob/master/4th/slide.md
# もし、正規表現を使わずに文字のパターンマッチをするとしたら・・・?

my $line = 'papix loves perl';

# 通常の正規表現
if ( $line =~ /perl/ ) {
    print "include\n";
}
else {
    print "not include\n";
}

# 正規表現を使わない場合
my @splitted_line = split //, $line;
for ( my $i = 0; $i < @splitted_line; $i++ ) {
    if ( $splitted_line[$i] eq 'p' ) {
        if ( $splitted_line[ $i + 1 ] eq 'a' ) {
            if ( $splitted_line[ $i + 2 ] eq 'p' ) {
                if ( $splitted_line[ $i + 3 ] eq 'i' ) {
                    if ( $splitted_line[ $i + 4 ] eq 'x' ) {
                        print "include\n";
                    }
                }
            }
        }
    }
}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.