I'm trying to split a string email that contains HTML content into 2 parts, one before <div class=\"gmail_signature\"> and one after.
EDIT: Here's a sample of what's in email:
My email is here<div class=\"gmail_quote\">and my signature</div>
So I'd like to have My email is here and <div class=\"gmail_quote\">and my signature</div> separately.
Using email.index('<div class=\"gmail_signature\">') returns nil, because of the \". The same issue happens with gsub.
What's the best way to deal with this?
Thanks!
email = 'aaa<div class="gmail_signature">bbb'
=> "aaa<div class=\"gmail_signature\">bbb"
email.split('<div class="gmail_signature">')
=> ["aaa", "bbb"]
Related
I have params like:
params[:id]= "\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\""
And i want to get expected result as below:
"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
How can I do this?
You can use gsub:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".gsub("\"", "")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
Or, as #Stefan mentioned, delete:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".delete("\"")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
If this is JSON data, which it could very well be in that format:
JSON.load(params[:id])
This handles things where there's somehow escaped strings in there, or the parameters are an array.
Just Use tr!
params[:id].tr!("\"","")
tr! will also change the main string
In case you do not want to change main string just use :
params[:id].tr("\"","")
Thanks Ilya
I have the string "{:name=>\"entry 1\", :description=>\"description 1\"}"
I'm using regex to get the values of name and description...
string = "{:name=>\"entry 1\", :description=>\"description 1\"}"
name = /\:name=>\"(.*?)\,/.match(string)
description = /\:description=>\"(.*?)\,/.match(string)
This however only returns name as #<MatchData ":name=>\"entry 1\"," 1:"entry 1\""> and description comes back as nil.
What I ideally want is for name to return "entry 1" and description come back as "description 1"
I'm not sure where I'm going wrong... any ideas?
The problem is the comma in /\:description=>\"(.*?)\,/ should be /\:description=>\"(.*?)/ or /\:description=>\"([^"]+)/
Also you can this method:
def extract_value_from_string(string, key)
%r{#{key}=>\"([^"]+)}.match(string)[1]
end
extract_value_from_string(string, 'description')
=> "description 1"
extract_value_from_string(string, 'name')
=> "name 1"
try this regex to retrieve both name and description at one step
(?<=name=>\\"|description=>\\")[^\\]+
try this Demo
I know this demo is using PCRE but I've tested also on http://rubular.com/ and it works fine
and if you want to get them separately use this regex is to extract name (?<=name=>\\")[^\\]+ and this for description (?<=description=>\\")[^\\]+
I'm trying to make a regex that removes me in my text email: toto#toto.com.
example: I request information on your project email: toto#free.fr
So I did this that captures me "email: toto#toto.com"
message ="I request information on your project email. toto#free.fr"
message.gsub!("/(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i")
it returns me nothing, and I wish there was just in the message text.
thanks
Try this. This should work for both uppercase, lowercase and emails appear in the middle of the string.
email = /[A-Za-z]{5}:\s[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/
s = "I request information on your project email: toto#free.fr"
s.match(email).pre_match #=> "I request information on your project "
s2 = "This email: blah#bLAH.com is in the middle"
s2.match(email).pre_match #=> "This "
s2.match(email).post_match #=> " is in the middle"
But there are more cases not covered e.g. email: followed by many spaces
Your code has several problems:
You are looking for "email: ...", but you message has "email. ...".
You use gsub!, with one parameter, which is not the classic use case, and returns an Enumerator. The classic use case expects a second parameter, which indicates to what you want to substitute the found matches:
Performs the substitutions of String#gsub in place, returning str, or
nil if no substitutions were performed. If no block and no replacement
is given, an enumerator is returned instead.
You pass a string to the gsub! - "/(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i", which is different than sending a regex. To pass a regex, you need to drop the quotes around it: /(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i
So a fix to your code would look like this:
message ="I request information on your project email: toto#free.fr"
message.gsub!(/(email: [-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$)/i, '')
# => "I request information on your project "
Also note I changed your code to use gsub instead of gsub!, since gsub! changes the underlying string, instead of creating a new one, and unless you have a good reason to do that, it is not encouraged to mutate the input arguments...
If you want to remove the email from the text use String#sub
message = "I request information on your project email. toto#free.fr"
message.sub!(/[A-Za-z]{5}:\s[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/, '')
# => "I request information on your project "
In Ruby, I want to replace a given URL in an HTML string.
Here is my unsuccessful attempt:
escaped_url = url.gsub(/\//,"\/").gsub(/\./,"\.").gsub(/\?/,"\?")
path_regexp = Regexp.new(escaped_url)
html.gsub!(path_regexp, new_url)
Note: url is actually a Google Chart request URL I wrote, which will not have more special characters than /?|.=%:
The gsub method can take a string or a Regexp as its first argument, same goes for gsub!. For example:
>> 'here is some ..text.. xxtextxx'.gsub('..text..', 'pancakes')
=> "here is some pancakes xxtextxx"
So you don't need to bother with a regex or escaping at all, just do a straight string replacement:
html.gsub!(url, new_url)
Or better, use an HTML parser to find the particular node you're looking for and do a simple attribute assignment.
I think you're looking for something like:
path_regexp = Regexp.new(Regexp.escape(url))
URI.extract claims to do this, but it doesn't handle matched parens:
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
What's the best way to extract URLs from text without breaking parenthesized URLs (which users like to use)?
If the URLs are always bound by parentheses a Regular Expression might be a better solution.
text = "text here (http://foo.example.org/bla) and here and here is (http://yet.another.url/with/parens) and some more text"
text.scan /\(([^\)]*)\)/
Before using this
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
You need to add this
require 'uri'
You could use this regexp to extract URL's from a string
"some thing http://abcd.com/ and http://google.com are great".scan(/(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:(?::[0-9]{1,5})?\/[^\s]*)?/ix)