[RUBY] I want to extract between character strings with a regular expression

It took a lot of time to extract between the character strings, so make a note.

Background

The front and back of the character string are fixed, and the characters in between are extracted.

Example:
I want to extract the service name part from the file name and constants concatenated below

COMPANY_SERVICE_OPTION
* Naming convention is service company_Service name_OPTION

pattern 1

Refer to here


"COMPANY_SERVICE_OPTION".slice(/COMPANY_(.+)_OPTION/)
puts $+

SERVICE

"$ +" Is a built-in variable of Ruby, and there are various other patterns. However, the following pattern 2 was used because there were some descriptions that it was deprecated.

Pattern 2

Refer to this gsub description


puts "COMPANY_SERVICE_OPTION".gsub(/COMPANY_(.+)_OPTION/,'\+')

SERVICE

Pattern 3 (additional note)

How to comment from scivola


"COMPANY_SERVICE_OPTION".match(/COMPANY_(.+?)_OPTION/)[1]

SERVICE

that's all. It will be encouraging if you like and follow Qiita and Twitter! If you have any other options, I'd love to hear from you. Thanking you in advance~

reference

I want to extract the character string between the regular expressions [Ruby] Write Kernel special variables without $ symbol as much as possible String --Ruby Reference Manual

Recommended Posts

I want to extract between character strings with a regular expression
I want to ForEach an array with a Lambda expression in Java
I want to monitor a specific file with WatchService
Extract a string starting with a capital letter with a regular expression (Ruby)
I want to make a button with a line break with link_to [Note]
Replace with a value according to the match with a Java regular expression
I want to develop a web application!
I want to write a nice build.gradle
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to display a PDF in Chinese (Korean) with thin reports
I want to test Action Cable with RSpec test
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to simply write a repeating string
I want to design a structured exception handling
I tried to break a block with java (1)
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to be able to read a file using refile with administrate [rails6]
Rails6 I want to make an array of values with a check box
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to get a list of only unique character strings by excluding fixed character strings from the file name
I tried to express the phone number (landline / mobile phone) with a regular expression in Rails and write validation and test
I want to call a method of another class
Pointcut Expression I want to specify more than one
I want to use a little icon in Rails
I want to dark mode with the SWT app
I want to authenticate users to Rails with Devise + OmniAuth
I want to define a function in Rails Console
I want to transition screens with kotlin and java!
Ruby Regular Expression Extracts from a specific string to a string
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I want to get along with Map [Java beginner]
[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
I want to redirect sound from Ubuntu with xrdp
[Ruby/Rails] How to generate a password in a regular expression
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
I want to write a loop that references an index with Java 8's Stream API
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
I want to push an app made with Rails 6 to GitHub
I want to implement a product information editing function ~ part1 ~
I made a plugin to execute jextract with Gradle task
I want to make a specific model of ActiveRecord ReadOnly
I want to call a method and count the number
I want to create a form to select the [Rails] category
I want to give a class name to the select attribute
I want to manually send an authorization email with Devise
I want to distinct the duplicated data with has_many through
I want to implement various functions with kotlin and java!