Created
March 16, 2018 08:42
-
-
Save trickster/17e30f4c044b8fd94d5cd762ed22d36a to your computer and use it in GitHub Desktop.
Regex perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# p6 | |
$_ = 'my123123 2replacements 7123'; | |
s/\d+//; # extract the first occurance of the numbers | |
# `m` is for matching | |
if 'properly' ~~ m/ perl / { | |
say "properly contains 'perl'" | |
} | |
'properly' ~~ m/ perl /; | |
'two words are nothing' ~~ m/ 'two words' /; | |
'rwo wirds' ~~ m/ 'rwo wirds' /; | |
'abcdef' ~~ m/ de /; | |
'abcdef' ~~ / de /; | |
if 'abcdef' ~~ / de / { | |
say ~$/; | |
say $/.prematch; | |
say $/.postmatch; | |
say $/.from; | |
say $/.to; | |
}; | |
# . matches any char | |
'perl' ~~ /per./; | |
'perl' ~~ /per ./; #whitespace ignored | |
'speller' ~~ / p..l /; | |
my $text = qq:to/END/ | |
Although I am a | |
multi-line text, | |
now can be matched | |
with /.*/. | |
END | |
; | |
say $text ~~ / .* /; | |
'ab42' ~~ /\d/ and say ~$/; # first digit | |
# \h, \s whitespace | |
# \d digit | |
# \S non whitespace chars | |
# \w alnum | |
'ab42cd1' ~~ /\d+/ and say ~$/; | |
'ab42cd1' ~~ /<digit>+/ and say ~$/; | |
'contains a word starting with "w"' ~~ /'d s' \S+/ and say ~$/; | |
'contains a word starting with "w"' ~~ /w \S+/ and say ~$/; | |
"a".uniprop('Script'); | |
"平仮名".uniprop('Script'); | |
'asA' ~~ /<:Lu>/ and say ~$/; # A | |
'asA' ~~ /<:!Lu>/ and say ~$/; # a | |
# To match either a lower-case letter or a number, write <:Ll+:N> | |
'perl6' ~~ /\w+(<:Ll+:N>)/; # 0 => 6 Does it loop and return the last element | |
"abacabadabacabc1" ~~ / <[ abc1 ]> /; | |
# the first matches a quote, then any char that aren't quotes | |
# | |
'"in quotes"' ~~ / '"' <-[ " ]> * '"'/ and say ~$/; | |
'"in quotes"' ~~ / '"' <-[ " ]> + '"'/ and say ~$/; | |
# Quantifiers | |
# a+ matches one or more a characters | |
# a* matches zero or more a characters | |
# a? matches zero or one | |
"bwerh3j45=100" ~~ / \w+ '=' \w+ /; | |
# ** no of times to match | |
'abcdefg' ~~ /\w ** 4/; | |
"123,456,789" ~~ / [\d+] ** 1 % ',' $ /; | |
if "123,456,789" ~~ / ([\d+]) ** 3 % ',' / { | |
say $/.list[0][1]; | |
}; | |
say so 'properly use perl' ~~ / perl/; | |
say so 'perly' ~~ /^ perl/; | |
say so 'perly' ~~ /ly $/; | |
# word boundary | |
'quick brown' ~~ /<< br/; | |
my $str = "123,456,789"; | |
if $str ~~ / (\d+) ',' (\d+) ',' (\d+) / { | |
say $1; | |
say $/.list[1]; | |
say ~$1; | |
}; | |
if "123,456,789" ~~ / ([\d+]) ** 3 % ',' / { | |
say $0[1]; | |
say ~$0[1]; | |
say ~$/.list[0][1]; | |
}; | |
$str = "siva is 2 and koki is 5" ; | |
say $str ~~ s/2/5/; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment