Want to get string between specialchars at endof the string

Hi friends,
I have string like
“tes

ubyrails [email protected]
From the above string i always should get the text between chars <>
from the string .I should get chars should be between chars which are
in endof string only
I want result should be as 2 strings
  1. [email protected]
    2 tesubyrails

The quick way to do that would be:

“tes

ubyrails [email protected]”.split(" <").each do |el| puts
el[0…-2] end

If you wanted something a bit more flexible I recommend regex. Do you
have more examples, or is this static data you’re using?

On Fri, Sep 14, 2012 at 10:08 AM, Robert K.
[email protected] wrote:

irb(main):001:0> s = ‘tes

ubyrails [email protected]
irb(main):002:0> if %r{\A(.?)\s+(<.?>)\z} =~ s then front=$1;
rear=$2; p front, rear; end
“tes ubyrails”
[email protected]
=> [“tes ubyrails”, “[email protected]”]

Oops, he wanted the text between <>:

irb(main):005:0> if %r{\A(.?)\s+<(.?)>\z} =~ s then front=$1;
rear=$2; p front, rear; end
“tes

ubyrails”
[email protected]
=> [“tes ubyrails”, “[email protected]”]

Cheers

robert

On Fri, Sep 14, 2012 at 10:00 AM, Joel P. [email protected]
wrote:

The quick way to do that would be:

“tes

ubyrails [email protected]”.split(" <").each do |el| puts
el[0…-2] end

If you wanted something a bit more flexible I recommend regex.

Like

irb(main):001:0> s = ‘tes

ubyrails [email protected]
irb(main):002:0> if %r{\A(.?)\s+(<.?>)\z} =~ s then front=$1;
rear=$2; p front, rear; end
“tes ubyrails”
[email protected]
=> [“tes ubyrails”, “[email protected]”]

Do you have more examples, or is this static data you’re using?

That would certainly be helpful.

Kind regards

robert

str = “tes

ubyrails [email protected]

name, email = str.split
email = email[1…-2]

puts name, email

–output:–
tes

ubyrails
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs