Regular expression - excluding a character
Here is an example:
s="abcd+subtext@example.com"
s.match(/+[^@]*/)
Result =开发者_StackOverflow> "+subtext"
The thing is, i do not want to include "+" in there. I want the result to be "subtext", without the +
You can use parentheses in the regular expression to create a match group:
s="abcd+subtext@example.com"
s =~ /\+([^@]*)/ && $1
=> "subtext"
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.
This works for me:
\+([^@]+)
I like to use Rubular for playing around with regular expressions. Makes debugging a lot easier.
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
精彩评论