Extract a substring from a string in Ruby using a regular expression
String1.scan(/<([^>]*)>/).last.first
scan
creates an array which, for each <item>
in String1
contains the text between the <
and the >
in a one-element array (because when used with a regex containing capturing groups, scan creates an array containing the captures for each match). last
gives you the last of those arrays and first
then gives you the string in it.
Ruby - extracting a substring from a string
You can use gsub
:
arr = ["/currencies/avoncoin/"]
arr = arr.map{ |s| s.gsub("/currencies/", "").gsub("/","") }
best way to find substring in ruby using regular expression
If string = "http://stackoverflow.com"
,
a really easy way is string.split("http://")[1]
. But this isn't regex.
A regex solution would be as follows:
string.scan(/^http:\/\/(.+)$/).flatten.first
To explain:
String#scan
returns the first match of the regex.- The regex:
^
matches beginning of linehttp:
matches those characters\/\/
matches//
(.+)
sets a "match group" containing any number of any characters. This is the value returned by the scan.$
matches end of line
.flatten.first
extracts the results fromString#scan
, which in this case returns a nested array.
Get substring from string using regex in ruby
Running the regex in multiline mode should solve the issue:
(?m)Unhandled Exception:(.*?):
Code:
re = /Unhandled Exception:(.*?):/m
str = 'g4net:HostName=abc}
Unhandled Exception:
System.NullReferenceException: Object reference not set to an
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Extract a substring in Ruby using a regular expression
Here's a regex that will retrieve the field name and the value separately:
text = "A-field:name.23.134 => 6"
matches = text.match(/([^:]+:[^=\.\s]+)(\.\d+)*\s*=>\s*(.+)/)
puts "Field: #{matches[1]}"
puts "Value: #{matches[3]}"
puts "#{matches[1]} => #{matches[3]}"
The output of this is:
Field: A-field:name
Value: 6
A-field:name => 6
Extract substring from string using key pattern and delimiter using Ruby
You can use this regular expression:
mynames=([^\s]+)
And then, look for the first group: $1
Here is a live example in JavaScript (works also in other languages):
var regex = /mynames=([^\s]+)/;var text = "toto=1,2,3 mynames=ralf,john,franky myhobbies=tennis,soccer,naps"
console.log(regex.exec(text)[1]);
Ruby extract part of string
Tested, works
Using regex:
str = "CN=KendallLDAPTESTWeihe,OU=CORPUSERS,OU=CORP,DC=int,DC=appriss,DC=com"
str[/\=(.*?),/,1]
For anyone re-using this, replace both instances of here
with the characters you want to find the text in between:
[/\here(.*?)here/,1]
Related Topics
Ruby on Rails - Access Controller Variable from Model
How to Install Therubyracer Gem on 10.10 Yosemite
How to Implement a Short Url Like the Urls in Twitter
What Is a Regex to Match a String Not At the End of a Line
How to Install MySQL2 Gem on Windows 7
Ignoring Gem Because Its Extensions Are Not Built
How to Update Ruby on Linux (Ubuntu)
403 Forbidden on Rails App W/ Nginx, Passenger
What Is the "Right" Way to Iterate Through an Array in Ruby
Ruby - Access Multidimensional Hash and Avoid Access Nil Object
Can You Ask Ruby to Treat Warnings as Errors
How to Define Action With Simple Form For
Set Utf-8 as Default String Encoding in Heroku
Paperclip::Errors::Missingrequired
validatorerror With Rails 4