return true because first occurrence moving backwards from example is \n...">

Reverse regex for a specific string

"very \n simple example of big"
 => return true because first occurrence moving backwards from example is \n


"very \n\t\t simple example of big" 
 => return false because first occurrence moving backwards from example is \n\t\t

is this possible with reverse regex search?

+3
source share
4 answers

I'm still not quite sure what you are looking for, but if it is "find out whether the word" example "occurs on a line that does not start with any tabs", then here you go:

ruby-1.9.2-p136 :001 > samples = [ "very \n simple example of big", "very \n\t\t simple example of big" ]
 => ["very \n simple example of big", "very \n\t\t simple example of big"] 
ruby-1.9.2-p136 :002 > samples.map{ |s| s[/^[^\t\n]+.+?example/] }
 => [" simple example", nil] 
+1
source

I don't know Ruby, so I give a regex that I tested in Javascript:

/\n[^(\t+)]+ example/g
+1
source

, , , , :

^(?!\t\t).*example

: http://rubular.com/r/DRdlscH6cO

^ ( \n, , ). . , .
:

^(?!\t\t)(.*)\bexample\b

, example, \t\t. \t \t\t .

+1

, , :

>> "very \n simple example of big".reverse.match(/elpmaxe[^\t]+\n/)
=> #<MatchData "elpmaxe elpmis \n">

>> "very \n\t\t simple example of big".reverse.match(/elpmaxe[^\t]+\n/)
=> nil

, , .

+1

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


All Articles