0

I have some strings like the 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 I want to select "PD_" part to substitute it with "" (the void string) but only inf the entire line does not contain the string ".join."

what I want to 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 is not a problem since I'm using eclipse search tool and will hit replace as soon as it show me the right result.

I have tried:

^((?!\.join\.).)*(PD_)*$ // whole string selected 
^((?!\.join\.).)*(\bPD_\b)*$ // whole string selected 

I start getting frustrated since I've searched a bit around (the ^((?!join bla bla come from those searches)

Can you help me?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Alex75
  • 527
  • 6
  • 20
  • Try [`^(?!.*\.join\.)(.*)PD_` => `$1`](https://regex101.com/r/SG3IHE/3). I understand there is only 1 `PD_` on a line to remove? – Wiktor Stribiżew Jul 25 '17 at 13:52
  • nope ... it select the whole line again :( – Alex75 Jul 25 '17 at 13:54
  • 1
    Yes, it does, but replace with `$1` – Wiktor Stribiżew Jul 25 '17 at 13:54
  • Might be helpful: https://stackoverflow.com/questions/1372748/eclipse-regular-expression-search-and-replace – SpaceTrucker Jul 25 '17 at 13:56
  • 1) there can be multiple occurrence of PD_ in a single line since is a code line and PD_something is a class and have for sure PD_a_field and they are a lot 2) not suere if eclipse search tool can use $1 for substitution ... i'm not vriting code ... i'm substiting portion of code with void string (is for database table name changes) – Alex75 Jul 25 '17 at 13:58
  • @Alex75 OK, try the expression in the answer. If it overmatches, let me know. – Wiktor Stribiżew Jul 25 '17 at 14:10

1 Answers1

3

You may use the following regex:

(?m)(?:\G(?!\A)|^(?!.*\.join\.))(.*?)PD_

and replace with

$1

See the regex demo

Details:

  • (?m) - a Pattern.MULTILINE inline modifier flag that will force ^ to match the beginning of a line rather than a whole string
  • (?:\G(?!\A)|^(?!.*\.join\.)) - either of the two alternatives:
    • \G(?!\A) - the end of the previous successful match
    • | - or
    • ^(?!.*\.join\.) - start of a line that has no .join. text in it (as the (?!.*\.join\.) is a negative lookahead that will fail the match if it matches any 0+ chars other than line break chars (.*) and then .join.)
  • (.*?) - Capturing group #1 (referred to with the $1 backreference in the replacement pattern): any 0+ chars other than line breaks, as few as possible, up to the first occurrence of ...
  • PD_ - a literal PD_

The replacement is a $1 backreference to the first capturing group that will restore any text matched before PD_s.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563