library(tidyverse)vec <- c("1","120","34.3","ab123",
"5b","6 5","7","b","ac4235432",
"45.3mg/dl","abc500ml 3unit 3:40AM",
"^ is start.",
"this sign($) represents end.", "....")
vec [1] "1" "120"
[3] "34.3" "ab123"
[5] "5b" "6 5"
[7] "7" "b"
[9] "ac4235432" "45.3mg/dl"
[11] "abc500ml 3unit 3:40AM" "^ is start."
[13] "this sign($) represents end." "...."
ここでは、代表的な正規表現の記号を見ていきましょう。尚、\d+のように、「まとめて指定」するために利用する+のような文字をメタ文字と呼びます。この動画は代表的なメタ文字の解説です。
数字にこだわらず「なんでもよい」場合は「.」で表現できます。
str_view(vec,".") [1] │ <1>
[2] │ <1><2><0>
[3] │ <3><4><.><3>
[4] │ <a><b><1><2><3>
[5] │ <5><b>
[6] │ <6>< ><5>
[7] │ <7>
[8] │ <b>
[9] │ <a><c><4><2><3><5><4><3><2>
[10] │ <4><5><.><3><m><g></><d><l>
[11] │ <a><b><c><5><0><0><m><l>< ><3><u><n><i><t>< ><3><:><4><0><A><M>
[12] │ <^>< ><i><s>< ><s><t><a><r><t><.>
[13] │ <t><h><i><s>< ><s><i><g><n><(><$><)>< ><r><e><p><r><e><s><e><n><t><s>< ><e><n><d><.>
[14] │ <.><.><.><.>
str_view(vec,"....") [3] │ <34.3>
[4] │ <ab12>3
[9] │ <ac42><3543>2
[10] │ <45.3><mg/d>l
[11] │ <abc5><00ml>< 3un><it 3><:40A>M
[12] │ <^ is>< sta>rt.
[13] │ <this>< sig><n($)>< rep><rese><nts ><end.>
[14] │ <....>
str_view(vec,".{4,5}") [3] │ <34.3>
[4] │ <ab123>
[9] │ <ac423><5432>
[10] │ <45.3m><g/dl>
[11] │ <abc50><0ml 3><unit ><3:40A>M
[12] │ <^ is ><start>.
[13] │ <this ><sign(><$) re><prese><nts e>nd.
[14] │ <....>
str_view(vec,".+") [1] │ <1>
[2] │ <120>
[3] │ <34.3>
[4] │ <ab123>
[5] │ <5b>
[6] │ <6 5>
[7] │ <7>
[8] │ <b>
[9] │ <ac4235432>
[10] │ <45.3mg/dl>
[11] │ <abc500ml 3unit 3:40AM>
[12] │ <^ is start.>
[13] │ <this sign($) represents end.>
[14] │ <....>
パターンの開始が文字列の先頭にあるかを^で調べることもきます。
str_view(vec,"^b") #で先頭にbがあるか?[8] │ <b>
str_view(vec,"^a") #で先頭にaがあるか? [4] │ <a>b123
[9] │ <a>c4235432
[11] │ <a>bc500ml 3unit 3:40AM
str_view(vec,"^\\d") #で先頭が数字で始まっているか? [1] │ <1>
[2] │ <1>20
[3] │ <3>4.3
[5] │ <5>b
[6] │ <6> 5
[7] │ <7>
[10] │ <4>5.3mg/dl
同様に最後は$です。
str_view(vec,"M$") #文字列の最後がMで終わっているか?[11] │ abc500ml 3unit 3:40A<M>
str_view(vec,"\\d{3}$") #[2] │ <120>
[4] │ ab<123>
[9] │ ac4235<432>
^と$
str_view(vec,"^b$")[8] │ <b>
str_view(vec,"^\\d+$")[1] │ <1>
[2] │ <120>
[7] │ <7>
ところで、.とか^とか$とかを
str_view(vec,"^") [1] │ <>1
[2] │ <>120
[3] │ <>34.3
[4] │ <>ab123
[5] │ <>5b
[6] │ <>6 5
[7] │ <>7
[8] │ <>b
[9] │ <>ac4235432
[10] │ <>45.3mg/dl
[11] │ <>abc500ml 3unit 3:40AM
[12] │ <>^ is start.
[13] │ <>this sign($) represents end.
[14] │ <>....
ひっかけるにはどうしたらいいでしょうか?
\をつけることで、「これはメタ文字じゃなくて文字ですよ」とすることができます。この方法、
「\でエスケープする」と表現されることが多いです。
str_view(vec,"\\^")[12] │ <^> is start.
str_view(vec,"\\$")[13] │ this sign(<$>) represents end.
str_view(vec,"\\.+") [3] │ 34<.>3
[10] │ 45<.>3mg/dl
[12] │ ^ is start<.>
[13] │ this sign($) represents end<.>
[14] │ <....>
ちゃんとひっかけられていますね?
その他の書き方も解説しておきます
str_view(vec,"\\w+") #すべての文字 [1] │ <1>
[2] │ <120>
[3] │ <34>.<3>
[4] │ <ab123>
[5] │ <5b>
[6] │ <6> <5>
[7] │ <7>
[8] │ <b>
[9] │ <ac4235432>
[10] │ <45>.<3mg>/<dl>
[11] │ <abc500ml> <3unit> <3>:<40AM>
[12] │ ^ <is> <start>.
[13] │ <this> <sign>($) <represents> <end>.
str_view(vec,"\\W+") #すべての非文字 [3] │ 34<.>3
[6] │ 6< >5
[10] │ 45<.>3mg</>dl
[11] │ abc500ml< >3unit< >3<:>40AM
[12] │ <^ >is< >start<.>
[13] │ this< >sign<($) >represents< >end<.>
[14] │ <....>
str_view(vec,"\\d+") #すべての数字 [1] │ <1>
[2] │ <120>
[3] │ <34>.<3>
[4] │ ab<123>
[5] │ <5>b
[6] │ <6> <5>
[7] │ <7>
[9] │ ac<4235432>
[10] │ <45>.<3>mg/dl
[11] │ abc<500>ml <3>unit <3>:<40>AM
str_view(vec,"\\D+") #すべての非数字 [3] │ 34<.>3
[4] │ <ab>123
[5] │ 5<b>
[6] │ 6< >5
[8] │ <b>
[9] │ <ac>4235432
[10] │ 45<.>3<mg/dl>
[11] │ <abc>500<ml >3<unit >3<:>40<AM>
[12] │ <^ is start.>
[13] │ <this sign($) represents end.>
[14] │ <....>
str_view(vec,"\\s+") #スペース [6] │ 6< >5
[11] │ abc500ml< >3unit< >3:40AM
[12] │ ^< >is< >start.
[13] │ this< >sign($)< >represents< >end.
str_view(vec,"\\S+") #非スペース [1] │ <1>
[2] │ <120>
[3] │ <34.3>
[4] │ <ab123>
[5] │ <5b>
[6] │ <6> <5>
[7] │ <7>
[8] │ <b>
[9] │ <ac4235432>
[10] │ <45.3mg/dl>
[11] │ <abc500ml> <3unit> <3:40AM>
[12] │ <^> <is> <start.>
[13] │ <this> <sign($)> <represents> <end.>
[14] │ <....>
もっと色々ありますので、深く知りたいという方はRStudioのHPにあるCheat Sheetをご確認ください。