tags:

views:

72

answers:

4

Here is an example:

s="[email protected]"

s.match(/+[^@]*/)

Result => "+subtext"

The thing is, i do not want to include "+" in there. I want the result to be "subtext", with out the +

+2  A: 

You could use a positive lookbehind assertion, which I believe is written like this:

s.match(/(?<=\+)[^@]*/)

EDIT: So I just noticed this is a Ruby question, and I don't know if this feature is in Ruby (I'm not a Ruby programmer myself). If it is, you can use it; if not... I'll delete this.

David Zaslavsky
This will work only in Ruby 1.9+
mckeed
@mckeed: thanks.
David Zaslavsky
+4  A: 

You can use parentheses in the regular expression to create a match group:

s="[email protected]"
s =~ /\+([^@]*)/ && $1
=> "subtext"
Wayne Conrad
Another syntax for this is `s[/\+([^@]*)/, 1]`.
mckeed
@mckeed, I didn't know that. Very nice!
Wayne Conrad
ming yeow
Wayne Conrad
A: 

I don't know Ruby very well, but if you add capturing around the portion you want it should work. ie: \+([^@]*)

You can test these with Rubular. This specific match is here: http://www.rubular.com/r/pqFza9jlmX

drewk
A: 

This works for me:

\+([^@]+)

I like to use Rubular for playing around with regular expressions. Makes debugging a lot easier.

Jason Miesionczek