Ruby Regular Expression Extracts from a specific string to a string

Ruby regular expression How to extract from a specific string to a string

[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

When you want to get only the contents of [Contents]("Hogehoge (60g x 100 pieces) x 7 bags")

Conclusion

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"

Commentary (not so much ...)

[/ [Contents](. *?) [Delivery method] /, 1] [/ From here onwards! The character string (. *?) So far! String /] It means [/ character string ① (. *?) Character string ② /].

What is ~, 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.

Summary

[/ Character string ① (. *?) Character string ② /, 1]

Recommended Posts

Ruby Regular Expression Extracts from a specific string to a string
[Java] How to erase a specific character from a character string
Extract a string starting with a capital letter with a regular expression (Ruby)
Ruby regular expression
Transform from a normal class to a lambda expression
[Ruby] How to generate a random alphabet string
From Java to Ruby !!
[Ruby/Rails] How to generate a password in a regular expression
Convert to Ruby Leet string
Cut out a Ruby string
How to change a string in an array to a number in Ruby
How to store a string from ArrayList to String in Java (Personal)
Apply CSS to a specific View in Ruby on Rails
[ruby] Creating a program that responds only to specific conditions
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
[Ruby] Method to count specific characters
A little regular expression story Part 1
Replace with a value according to the match with a Java regular expression
Introduction to Ruby (from other languages)
A little regular expression story Part 2
I want to extract between character strings with a regular expression
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
Prevent login users from transitioning to a specific page by entering a URL
[Java] How to get to the front of a specific string using the String class
A story about converting character codes from UTF-8 to Shift-jis in Ruby
[Ruby] From the basics to the inject method
A program that calculates factorials from 2 to 100
Extract a part of a string with Ruby
Notation to put a variable in a string
I tried to build Ruby 3.0.0 from source
Type conversion from java BigDecimal type to String type
From terminal to Ruby (standard input / output)
StackOverflowError to String causes a circular reference
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
[Spring Boot] How to get properties dynamically from a string contained in a URL
I tried to get the distance from the address string to the nearest station with ruby
Use Stream # collect to retrieve and list only specific fields from a JavaBean list
How to start a subscript from an arbitrary number in Ruby iterative processing
Sample to create PDF from Excel with Ruby
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Scraping and writing specific elements to a file
Convert to a tag to URL string in Rails
Convert from C String pointer to Swift String type
[Android] How to convert a character string to resourceId
Connect to Aurora (MySQL) from a Java application
How to get a heapdump from a Docker container
Code to escape a JSON string in Java
Set the time of LocalDateTime to a specific time
Convert a string to a character-by-character array with swift
[Ruby] Regular expression for secure password policy setting
[Ruby] I want to do a method jump!
I want to simply write a repeating string
[ruby] How to receive values from standard input?
[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
[Java] Cut out a part of the character string with Matcher and regular expression
Extract elements by doing regular expression replacement from a lot of HTML with java