How to return the substring of a string between two strings in Ruby?
input_string = "blahblahblahSTARTfoofoofooENDwowowowowo"
str1_markerstring = "START"
str2_markerstring = "END"
input_string[/#{str1_markerstring}(.*?)#{str2_markerstring}/m, 1]
#=> "foofoofoo"
or to put it in a method:
class String
def string_between_markers marker1, marker2
self[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
end
end
"blahblahblahSTARTfoofoofooENDwowowowowo".string_between_markers("START", "END")
#=> "foofoofoo"
ruby, using regex to find something in between two strings
Between 1st +
and 1st @
:
to[/\+(.*?)@/,1]
Between 1st +
and last @
:
to[/\+(.*)@/,1]
Between last +
and last @
:
to[/.*\+(.*)@/,1]
Between last +
and 1st @
:
to[/.*\+(.*?)@/,1]
Regex match all strings between 2 characters
You forgot to escape the pipe (|
), which is now interpreted as the indicator for an alternation
Your regex with the escaped pipe:
(?<=\|).*?(?=>)
Here you can see the result
How to get text between two strings in ruby?
f
is a file descriptor - you want to match on the text in the file, which you read into line
. What I prefer to do instead of reading the text into an array (which is hard to regex on) is to just read it into one string:
contents = File.open(metadataPath) { |f| f.read }
contents.match(/==========================(.*)Primary Category/m)[1].strip
The last line produces your desired output:
-This is the text I want to get \n-It can have 1 or many lines\n-These equal signs are repeated throughout the file to separate sections"
Ruby Regex to capture everything between two strings (inclusive)
I believe you're looking for an non-greedy regex, like this:
/<div class="the_class">(.*?)<\/div>/m
Note the added ?
. Now, the capturing group will capture as little as possible (non-greedy), instead of as most as possible (greedy).
Ruby regex to capture part of content between two strings
Match all substrings between <code>
and </code>
and replace all \n
with <br>
in those matches only:
html = html.gsub(/<code>.*?<\/code>/m) { $~[0].gsub('\n', '<br>') }
Fetching the substring contained between two specific words
Simple regular expression would do:
@var = "Hi, I want to extract container_start **ONLY THIS DYNAMIC CONTENT** container_end from the message contained between the container_start and container_end "
@var[/container_start(.*?)container_end/, 1] # => " **ONLY THIS DYNAMIC CONTENT** "
Ruby get a list of all matching strings between two words using regex
string.scan(/MediaUrl":"(.+?)"/)
Find strings that match two regular expressions
Is there a quick way in general? No. What are "all the strings" that match these pairs of regular expressions:
/.*/
and/\d*/
? (There are infinitely many!)/\A\d{10}\z/
and/\A[0-8]{10}\z/
? (There are 3,486,784,401!)/\w+\d{2,4}@?([[:punct:]]|\w){2}/
and/(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)/
(I haven't even tried to work this out; my point is: you could provide arbitrarily complicated input!!)
...But for simple scenarios, such as what you've actually asked, it would be feasible to use this ruby gem:
/^A+\S{2}$/.examples(max_group_results: 999) & /^AB+\d{1}$/.examples(max_group_results: 999)
=> ["AB0", "AB1", "AB2", "AB3", "AB4", "AB5", "AB6", "AB7", "AB8", "AB9"]
Extract text between two tags using regex in Ruby
You can use:
html = '<a href="abgeordnete-1128-0----w8397.html" class="small_link">Berlin-Treptow-Köpenick</a>'
html[/>(.*)</, 1]
#=> "Berlin-Treptow-Köpenick"
When your HTML partials are more complex then I recommend using a libraries like Nokogiri:
html = '<a href="abgeordnete-1128-0----w8397.html" class="small_link">Berlin-Treptow-Köpenick</a>'
require 'nokogiri'
Nokogiri::HTML(html).text
#=> "Berlin-Treptow-Köpenick"
Related Topics
Chef and Erb Templates. How to Use Boolean Code Blocks
Run Code Only If Script Called from the Command Line
Is Returning a Value from 'Next' a Bad Idea
Raise Exception When Accessing Attributes That Doesn't Exist in Openstruct
How to Negate a Scope in Rails
Deleting the Current Session with Rack::Session::Cookie
Ruby Array to Hash: Each Element the Key and Derive Value from It
Ruby Net::Smtp - Send Email with Bcc: Recipients
Ruby: Remove Whitespace Chars at the Beginning of a String
Where to Insert Rack::Deflater in the Rack
Rails 12 Hour Am/Pm Range for a Day