perl regex negative lookahead replacement with wildcard

Updated with real and more complicated task:

The problem is to substitute a certain pattern to different result with or without _ndm.

The input is a text file with certain line like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/<random path>/PAT1/<txt w/o _ndm>
/<random path>/PAT1/<txt w/ _ndm>
</code>
<code>/<random path>/PAT1/<txt w/o _ndm> /<random path>/PAT1/<txt w/ _ndm> </code>
/<random path>/PAT1/<txt w/o _ndm>
/<random path>/PAT1/<txt w/ _ndm>

I need change those to

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/<ramdom path>/PAT1/<sub1>
/<random path>/PAT1/<sub2>_ndm
</code>
<code>/<ramdom path>/PAT1/<sub1> /<random path>/PAT1/<sub2>_ndm </code>
/<ramdom path>/PAT1/<sub1>
/<random path>/PAT1/<sub2>_ndm

I wrote a perl command to process it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>perl -i -pe 's#PAT1/.*_ndm#<sub2>_ndm#; s#PAT1/.*(?!_ndm)#<sub1>#' <input_file>
</code>
<code>perl -i -pe 's#PAT1/.*_ndm#<sub2>_ndm#; s#PAT1/.*(?!_ndm)#<sub1>#' <input_file> </code>
perl -i -pe 's#PAT1/.*_ndm#<sub2>_ndm#; s#PAT1/.*(?!_ndm)#<sub1>#' <input_file>

However, it doesn’t work as expected. the are all substituted to instead of _ndm.


Original post:

I need use shell command to replace any string not ending with _ndm to another string (this is an example):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>abc
def
def_ndm
</code>
<code>abc def def_ndm </code>
abc
def
def_ndm

to

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ace
ace
def_ndm
</code>
<code>ace ace def_ndm </code>
ace
ace
def_ndm

I tried with perl command

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>perl -pe 's/.*(?!_ndm)/ace/'
</code>
<code>perl -pe 's/.*(?!_ndm)/ace/' </code>
perl -pe 's/.*(?!_ndm)/ace/'

However I found wildcard didn’t work with negative lookahead as my expected. Only if I include wildcard in negative pattern, it can skip def_ndm correctly; but because negative lookahead is a zero length one, it can’t replace normal string any more.

any idea?

6

Daisy chained matches

Force PAT1 to be the parent directory and then see whether the final name ends in _ndm. Note the need to squirrel away values of match variables $1 and $2 into $a and $b because the second match (against /_ndm$/) wipes out the previous capture variables.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>while (<>) {
chomp;
if (s#(/PAT1/)([^/]+)$#($a,$b) = ($1, $2); $a . ($b =~ /_ndm$/ ? "sub2_ndm" : "sub1")#e) {
print $_, "n";
}
else {
print "No match: $_n";
}
}
</code>
<code>while (<>) { chomp; if (s#(/PAT1/)([^/]+)$#($a,$b) = ($1, $2); $a . ($b =~ /_ndm$/ ? "sub2_ndm" : "sub1")#e) { print $_, "n"; } else { print "No match: $_n"; } } </code>
while (<>) {
  chomp;
  if (s#(/PAT1/)([^/]+)$#($a,$b) = ($1, $2); $a . ($b =~ /_ndm$/ ? "sub2_ndm" : "sub1")#e) {
    print $_, "n";
  }
  else {
    print "No match: $_n";
  }
}

Try it online.

Check which alternative matched

Find PAT1 as above and then give two alternatives for how the match can succeed. Note the use of named patterns to avoid having to count left-parentheses and a fixed-width negative look-behind for the _ndm case. Strictly speaking, only one of the look-behind assertions is necessary, but you may prefer keeping both to emphasize what’s happening.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>while (<>) {
chomp;
if (s{(?<prefix>/PAT1/) (?: (?<ndm>[^/]+)(?<=_ndm) | (?<nondm>[^/])+(?<!_ndm) )$}
{$+{prefix} . (exists $+{nondm} ? "sub1" : "sub2_ndm")}ex)
{
print $_, "n";
}
else {
print "No match: $_n";
}
}
</code>
<code>while (<>) { chomp; if (s{(?<prefix>/PAT1/) (?: (?<ndm>[^/]+)(?<=_ndm) | (?<nondm>[^/])+(?<!_ndm) )$} {$+{prefix} . (exists $+{nondm} ? "sub1" : "sub2_ndm")}ex) { print $_, "n"; } else { print "No match: $_n"; } } </code>
while (<>) {
  chomp;
  if (s{(?<prefix>/PAT1/) (?: (?<ndm>[^/]+)(?<=_ndm) | (?<nondm>[^/])+(?<!_ndm) )$}
       {$+{prefix} . (exists $+{nondm} ? "sub1" : "sub2_ndm")}ex)
  {
    print $_, "n";
  }
  else {
    print "No match: $_n";
  }
}

Try it online.

Output

Given input of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/foo/bar/PAT1/veeblefetzer
/baz/PAT1/quux_ndm
/nope/nope
/also/nope/PAT1/dir/dir/dir_ndm
</code>
<code>/foo/bar/PAT1/veeblefetzer /baz/PAT1/quux_ndm /nope/nope /also/nope/PAT1/dir/dir/dir_ndm </code>
/foo/bar/PAT1/veeblefetzer
/baz/PAT1/quux_ndm
/nope/nope
/also/nope/PAT1/dir/dir/dir_ndm

both output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/foo/bar/PAT1/sub1
/baz/PAT1/sub2_ndm
No match: /nope/nope
No match: /also/nope/PAT1/dir/dir/dir_ndm
</code>
<code>/foo/bar/PAT1/sub1 /baz/PAT1/sub2_ndm No match: /nope/nope No match: /also/nope/PAT1/dir/dir/dir_ndm </code>
/foo/bar/PAT1/sub1
/baz/PAT1/sub2_ndm
No match: /nope/nope
No match: /also/nope/PAT1/dir/dir/dir_ndm

0

As you’ve discovered, "PAT1/txt_ndm" =~ m#PAT1/.*(?!_ndm)# matches. Specifically, it matches PAT1/txt_ndm. Note that PAT1/txt_ndm isn’t followed by _ndm.

To only match the text up to _ndm (PAT1/txt), you can use the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PAT1/(?:(?!_ndm).)*
</code>
<code>PAT1/(?:(?!_ndm).)* </code>
PAT1/(?:(?!_ndm).)*

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật