java - regular expression retrieve a portion of a string that not contain a string -


i have strings following:

it.mycompany.db.beans.str1.pd_t_class it.mycompany.db.beans.join.pd_t_class it.mycompany.db.beans.str2.pd_t_class_1 it.mycompany.db.beans.join.pd_t_class_1 pd_t_class myvar = new pd_t_class(); myvar.setpd_t_class(something); 

and want select "pd_" part substitute "" (the void string) inf entire line not contain string ".join."

what want achieve is:

it.mycompany.db.beans.str1.t_class it.mycompany.db.beans.join.pd_t_class it.mycompany.db.beans.str2.t_class_1 it.mycompany.db.beans.join.pd_t_class_1 t_class myvar = new t_class(); myvar.sett_class(something); 

the substitution not problem since i'm using eclipse search tool , hit replace show me right result.

i have tried:

^((?!\.join\.).)*(pd_)*$ // whole string selected  ^((?!\.join\.).)*(\bpd_\b)*$ // whole string selected  

i start getting frustrated since i've searched bit around (the ^((?!join bla bla come searches)

can me?

you may use following regex:

(?m)(?:\g(?!\a)|^(?!.*\.join\.))(.*?)pd_ 

and replace with

$1 

see regex demo

details:

  • (?m) - pattern.multiline inline modifier flag force ^ match beginning of line rather whole string
  • (?:\g(?!\a)|^(?!.*\.join\.)) - either of 2 alternatives:
    • \g(?!\a) - end of previous successful match
    • | - or
    • ^(?!.*\.join\.) - start of line has no .join. text in (as (?!.*\.join\.) negative lookahead fail match if matches 0+ chars other line break chars (.*) , .join.)
  • (.*?) - capturing group #1 (referred $1 backreference in replacement pattern): 0+ chars other line breaks, few possible, first occurrence of ...
  • pd_ - literal pd_

the replacement $1 backreference first capturing group restore text matched before pd_s.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -