"+ subtext...">

Regular expression - excluding character

Here is an example:

s = " abcd+subtext@example.com "

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

Result => "+ subtext"

The fact is that I do not want to include "+" there. I want the result to be β€œsubtext”, without +

+4
source share
4 answers

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

s=" abcd+subtext@example.com " s =~ /\+([^@]*)/ && $1 => "subtext" 
+5
source

You can use the positive lookbehind statement, which I believe is written like this:

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

EDIT: I just noticed that this is a Ruby question, and I don’t know if this feature is included in Ruby (I'm not a Ruby programmer myself). If so, you can use it; if not ... I will remove it.

+2
source

This works for me:

 \+([^@]+) 

I like to use Rubular for regex games. Makes debugging a lot easier.

+2
source

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

You can check them with Rubular. This particular match is here: http://www.rubular.com/r/pqFza9jlmX

+1
source

Source: https://habr.com/ru/post/1315575/


All Articles