Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression - excluding a character

Tags:

regex

ruby

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", without the +

like image 264
meow Avatar asked Jul 13 '10 19:07

meow


2 Answers

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

s="[email protected]"
s =~ /\+([^@]*)/ && $1
=> "subtext"
like image 156
Wayne Conrad Avatar answered Oct 13 '22 00:10

Wayne Conrad


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.

like image 35
David Z Avatar answered Oct 13 '22 00:10

David Z