[Name] Hogefuga [Raw materials] Hogehoge, Fugafuga, Mogemoge [Contents] Hogehoge (60g x 100 pieces) x 7 bags [Delivery method] Fugafu ga delivery [Expiration date] Date of manufacture [Sales / Provider] Fugafuga Sales Office
I was a little addicted to how to write a regular expression when trying to extract only a specific character string from ↑, so I will introduce it
extract.rb
str = "[Name] Hogehoge [Raw materials] Hogehoge, Fugafuga, Mogemoge [Contents] Hogehoge(60g x 100 pieces)× 7 bags [Delivery method] Fugafuga delivery [Expiration date] Hogehoge day from the date of manufacture [Sales / providers] Fugafuga sales office"
str[/【Internal capacity】(.*?)【Shipping method】/, 1]
#Output result:"Hogehoge(60g x 100 pieces)× 7 bags"
[/ [Contents](. *?) [Delivery method] /, 1]
[/ From here onwards! The character string (. *?) So far! String /]
It means [/ character string ① (. *?) Character string ② /]
.
~, 1]
If you want to get the result without arguments, set the second argument to 1.
If the second argument is 0 or not specified
[Contents] Hogehoge (60g x 100 pieces) x 7 bags [Delivery method]
Will be returned.
[/ Character string ① (. *?) Character string ② /, 1]
Recommended Posts