It took a lot of time to extract between the character strings, so make a note.
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
"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.
Refer to this gsub description
puts "COMPANY_SERVICE_OPTION".gsub(/COMPANY_(.+)_OPTION/,'\+')
SERVICE
"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~
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