Regular Expression (re) คือการเขียน sequence of characters เพื่อ match pattern ที่อยู่ในข้อความ (text, string) ในตัวอย่างด้านล่างคือ ^H[a-z]{4}
เพื่อใช้ match คำว่า “Hello”
text <- "Hello World"
str_view(text, "^H[a-z]{4}")
tutorial วันนี้เราจะมาสอนเขียน re ง่ายๆใน R และลองใช้ฟังชั่น str_view
ของ package stringr เพื่อดู matched pattern ที่เราต้องการ
Basic matches

re แบบง่ายที่สุดคือเราเขียน characters ที่เราต้องการ match ได้เลย เช่น “cat” แบบนี้
text <- c("Cat", "cat", "cot")
str_view(text, "cat")
RStudio จะแสดงผลการ match ให้เราดูในหน้าต่าง view (ขวาล่างของหน้าจอ)

การเขียน re เป็นแบบ case sensitive แปลว่าเราต้องระบุให้ชัดเจนว่าอยากจะ match lower หรือว่า upper case
ถ้าอยากจะ match ทั้งคำว่า “cat” และ “Cat” เราสามารถใช้สัญลักษณ์ | เข้ามาช่วย และเขียน characters ที่ต้องการ match ในวงเล็บแบบนี้ “(C|c)”
str_view(text, "Cat")
str_view(text, "(C|c)at")

ถ้าอยากจะ match character อะไรก็ได้ 1 ตัว (any character) เราสามารถใช้ .
แบบนี้
str_view(text, "(C|c).t")

Digits

เราสามารถเขียน match ตัวเลข digits ได้ด้วย [0-9] หรือใช้ \\d ก็ได้ผลเหมือนกัน โดย \\d เรียกว่า metacharacter ซึ่งเป็นอักขระพิเศษของ re
text <- c("Hello", "World", "555", "12345")
str_view(text, "[0-9]+")
str_view(text, "\\d+")
สังเกต ว่าในโค้ดตัวอย่างด้านบนเราใส่ +
ต่อท้าย [0-9]+ และ \\d+ ด้วย ในภาษาของ re เราเรียก + ว่า quantifier ใช้ในการ match “one or more” ตัวอย่างเช่น [0-9]+ หรือ \\d+ สามารถ match ตัวเลขเหล่านี้ได้หมดเลย 6, 123, 5555, 98982 เป็นต้น
metacharacter ตัวหลักๆที่ควรรู้จัก เราสรุปมาให้แล้วในตารางด้านล่าง
metacharacter | ความหมาย |
\\d | match ตัวเลข 0-9 เทียบเท่ากับการเขียน [0-9] |
\\D | match อะไรก็ได้ที่ไม่ใช่ตัวเลข 0-9 เทียบเท่ากับการเขียน [^0-9] |
\\s | match white space character |
\\S | match non white space character |
\\n | match a newline character (ขึ้นบรรทัดใหม่) |
\\t | match a tab character |
Spaces

มาลองเขียน re เพื่อ match white space กันบ้าง
text <- c("Hello World", "School", "University of Thailand")
str_view(text, "\\s") # white space

ฟังชั่น str_view
จะ match เฉพาะ pattern แรกที่มันเจอเท่านั้น ถ้าอยากจะ match ทุก white space ใน text นั้นๆ ให้เปลี่ยนไปใช้ฟังชั่น str_view_all
str_view_all(text, "\\s")

R code
เพื่อนๆสามารถ copy R script ไปลองเล่นใน RStudio ได้เลย โดยฟังชั่น str_*
มาจาก package stringr รันโค้ด line 2 เพื่อติดตั้งแพ็คเกตก่อนใช้งาน
# intro to stringr | |
install.packages("stringr") | |
library(stringr) | |
# check state.name | |
print(state.name) | |
# most used function in stringr | |
str_view(state.name, pattern = "New", match = T) | |
# create an example string | |
text <- "DataRockie is the coolest DS school on the internet, founded in Y2015. | |
It offers free online courses on datarockie.com, go check it!" | |
# basic regular expressions | |
str_view(text, "DataRockie") | |
str_view(text, "2015") | |
str_view(text, "datarockie") | |
# this character | means OR | |
# look for words 'DataRockie' OR 'datarockie' | |
str_view_all(text, "(D|d)ata(R|r)ockie") | |
# look for digits | |
str_view(text, "[0-9]") | |
str_view(text, "[0-9]+") | |
str_view(text, "\\d") | |
str_view(text, "\\d+") | |
# look for English alphabet | |
str_view_all(text, "[a-z]+") | |
str_view_all(text, "[A-Z]+") | |
# look for space | |
str_view_all(text, "\\s") | |
# split text by " " | |
text_split <- str_split(text, pattern = " ")[[1]] | |
text_split | |
# look for words start with 'i' | |
str_view_all(text_split, "^i.+") | |
# look for words end with 's' | |
str_view_all(text_split, ".+s$") |
สรุป ^H[a-z]{4}
ที่เราเห็นใน introduction ของบทความนี้เป็นการเขียน re เพื่อ match คำที่ขึ้นต้นด้วยตัว H ^H
ตามด้วย lower case [a-z]
อีก 4 ตัว {4}
match คำว่า Hello เป๊ะ!

stringr ยังมีอีกหลายฟังชั่นที่ใช้ manipulate text ได้ง่ายๆ ตัวอย่างเช่นการทำ extract & replace string ลองอ่านการใช้งาน regular expression แบบละเอียดได้ที่นี่ – Hadley Wickham
install.packages(“stringr”) ไม่ผ่านอ่ะค่ะ ต้องทำยังไงคะ
ลองปิดแล้วเปิดโปรแกรมใหม่ได้ไหมครับ
install.packages(“stringr”, dependencies=T)
ปล. error มันเขียนว่าอะไรหรอครับที่ลงไม่ผ่าน
ได้แล้วค่าาา ขอบคุณมากค่ะ ^^
error ที่ติดตั้งไม่ได้
tar: Failed to set default locale
The downloaded binary packages are in /var/folders/c3/xdvwhn3564s3kj8bdry9mw4w0000gn/T//RtmpR7vo4d/downloaded_packages