Regex Syntax If Clause HELP

Hello,
I’m kinda new to this but I’m looking for a syntax which does the following (Person related Data is XXXX:

MARIS|DOC|XXXXX|GYN endgl. Brief ambulante OP|XXXXX|XXXX|8228284|1889286|XXXXXX12|XXXXX|0003999998||null|20190719|ZWMAMB

What I Want: If Text includes “GYN” replace “null” with “GYN”

I’m using grepwin, which has a seperate column for the replaced word (like https://regex101.com/)

Thank you in advance

Does null always come after GYN, or is it sometimes before?

Yes it does.

Another example:

MARIS|DOC|XXXXXX|CH1 endgültiger LWS Arztbrief|XXXXXX|XXXXXX|8093602|XXXXXX|XXXX|2XXXXXX4|0003999998||null|20190724|ZWMAB

Here “null” has to be replaced with “CH1” if text contains “CH1”

Ah that makes it easier.

/^(.*)(GYN)(.*)null(.*)$/
  1. start of string
  2. 0 or more of any character
  3. The literal string GYN
  4. 0 or more of any character
  5. The literal string null
  6. 0 or more of any character
  7. end of string

If the string does not contain GYN, the regex will not match. Everything except null (which you are going to replace) is in a capture group.

Replace with:

$1$2$3GYN$4

That’s the first capture group, the second capture group, the third capture group, GYN (which will replace the null, which is not captured), the fourth capture group.

Note that this assumes that the strings to test are each on a single line, no line breaks

Thank you very much. The language being used in grepwin is Perl. I translated it into following:

qr/^(.)(GYN)(.)null(.*)$/p

Following code was used:

strict;

my $str = ‘MARIS|DOC|XXXXX|GYN endgl. Brief ambulante OP|XXXXX|XXXX|8228284|1889286|XXXXXX12|XXXXX|0003999998||null|20190719|ZWMAMB’;
my $regex = qr/^(.)(GYN)(.)null(.*)$/p;
my $subst = ‘GYN’;

my $result = $str =~ s/$regex/$subst/r;

print “The result of the substitution is’ $result\n”;

Somehow - even if I’m typing in “GYN” in the seperated column of grepwin, it does not work.

May I didnt understand you? Sorry if so, I’m really new to this and finding my way through.

No, you understand, it’s just the substitution syntax I think.

In JS, it’s a dollar sign followed by the index (from 1) of the capturing group - so $1$2 is the first two, $2$1 would swap them and so on.

But $ already has meaning in Perl, so I don’t think it can be that – I’m not familiar with Perl at all, so not much help here at the minute, I’ll have a Google around

Edit: it is $

It’s because you’re not replacing the whole string I think, but my lack of Perl knowledge means I’m not sure what it should look like

Basically, it needs to be like

$subst = /$1$2$3GYN$4/

What’s between the / there is what the substitution should be, but I have no idea if using / is correct there.

Basically I think you want

$result = $str =~ s/$regex/$1$2$3GYN$4/r;