how can I perform both negative lookahead and negative lookbehind in a single perl regx?

Jacob Wegelin

In a multiline string, in each line, I want to delete everything from the first unescaped percent sign to the end of the line; with one exception. If the unescaped percent sign occurs in the following position: \d\d:\d\d%:\d\d, then I want to leave it alone.

(The string is LaTeX / TeX code and the percent sign denotes a comment. I want to treat a comment inside an HH:MM:SS string as a special case, where seconds were commented out of a time string.)

The code below manages almost to do it:

  1. it uses one negative lookbehind to leave \% alone
  2. it uses "ungreedy" to match the first, not last, %
  3. it uses another negative lookbehind to skip \d\d:\d\d%
  4. BUT it fails to differentiate between \d\d:\d\d%anything and \d\d:\d\d%\d\d, skipping both.
  5. My attempts at adding negative lookahead do not help. Is there a way to do this?
#!/usr/bin/perl
use strict; use warnings;

my $string = 'for 10\% and %delete-me
for 10\% and 2021-03-09 Tue 02:59%:02 NO DELETE %delete-me
for 10\% and 2021-03-09 Tue 04:09%anything  %delete-me
for 10 percent%delete-me';

print "original string:\n";
print "$string<<\n";

{
    my $tochange = $string;
    $tochange =~ s/
        (^.*?
        (?<!\\)
        )
        (\%.*)
        $/${1}/mgx;
    print "\ndelete after any unescaped %\n";
    print "$tochange<<\n";
}

{
    my $tochange = $string;
    $tochange =~ s/
        (^.*?
        (?<!\d\d:\d\d)
        (?<!\\)
        )
        (\%.*)
        $/${1}/mgx;
    print "\nexception for preceding HH:MM\n";
    print "$tochange<<\n";
}

{
    my $tochange = $string;
    $tochange =~ s/
        (^.*?
        (?<!\d\d:\d\d)
        (?<!\\)
        )
        (!?:\d\d)
        (\%.*)
        $/${1}/mgx;
    print "\nattempt to add negative lookahead\n";
    print "$tochange<<\n";
}


{
    my $tochange = $string;
    # attempt to add negative lookahead
    $tochange =~ s/
        (^.*?
        (?<!\d\d:\d\d)
        (?<!\\)
        )
        (\%.*)
        (!?:\d\d)
        $/${1}/mgx;
    print "\nattempt to add negative lookahead\n";
    print "$tochange<<\n";
}

The fourth bird

You might make use of SKIP FAIL approach:

\d\d:\d\d%:\d\d(*SKIP)(*FAIL)|(?<!\\)%.*
  • \d\d:\d\d%:\d\d(*SKIP)(*FAIL)| Match the pattern that you want to avoid
  • (?<!\\)%.* Negative lookbehind, assert not \ directly to the left and match % followed by the rest of the line

Regex demo | Perl demo

For example

$tochange =~ s/\d\d:\d\d%:\d\d(*SKIP)(*FAIL)|(?<!\\)%.*//g;

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Regex (negative lookahead)

분류에서Dev

Negative lookahead on multiple strings

분류에서Dev

Negative lookahead on multiple strings

분류에서Dev

Struggling with negative lookahead in a regex

분류에서Dev

Regex Negative Lookahead 문제

분류에서Dev

Negative Lookahead 및 Lookbehind가 모두 실패하는 경우에만 일치

분류에서Dev

Regex Negative Lookbehind 무시 됨

분류에서Dev

Python regex negative lookbehind not failing match

분류에서Dev

Scala regex multiline match with negative lookahead

분류에서Dev

Perl lookahead 및 lookbehind에 의해 혼동 됨

분류에서Dev

R의 Regex Negative lookbehind 케이스

분류에서Dev

How do I check if all elements of DataFrame are non-negative?

분류에서Dev

How do I use fill color for negative values in spider chart?

분류에서Dev

How can i calculate sensitivity(True positive rate) and specificity(True negative rate) using libsvm as a binary classifier in matlab?

분류에서Dev

Regex Lookahead & Lookbehind with Java

분류에서Dev

How can I perform an OR with ScalaTest

분류에서Dev

How to make negative rules in Apache?

분류에서Dev

Negative Lookbehind Regex가 PHP에서 작동하지 않습니다.

분류에서Dev

Negative Lookahead Regex가 내 문장을 무시합니다.

분류에서Dev

Python3 re lookahead negative가 작동하지 않음

분류에서Dev

for-loop to foreach with negative (i--) 문

분류에서Dev

Karate - how to wait for assertion (negative assertion)

분류에서Dev

How do you multiply negative numbers in javascript?

분류에서Dev

How to convert a positive number to negative in batch?

분류에서Dev

Regex Negative Lookbehind, 다른 단어가 앞에 나오지 않는 한 단어 일치

분류에서Dev

Lookahead 및 Lookbehind 정규식

분류에서Dev

Regex-Lookahead / LookBehind 어설 션

분류에서Dev

How can I join a nested Perl hash?

분류에서Dev

How i can handle http requests in perl?

Related 관련 기사

  1. 1

    Regex (negative lookahead)

  2. 2

    Negative lookahead on multiple strings

  3. 3

    Negative lookahead on multiple strings

  4. 4

    Struggling with negative lookahead in a regex

  5. 5

    Regex Negative Lookahead 문제

  6. 6

    Negative Lookahead 및 Lookbehind가 모두 실패하는 경우에만 일치

  7. 7

    Regex Negative Lookbehind 무시 됨

  8. 8

    Python regex negative lookbehind not failing match

  9. 9

    Scala regex multiline match with negative lookahead

  10. 10

    Perl lookahead 및 lookbehind에 의해 혼동 됨

  11. 11

    R의 Regex Negative lookbehind 케이스

  12. 12

    How do I check if all elements of DataFrame are non-negative?

  13. 13

    How do I use fill color for negative values in spider chart?

  14. 14

    How can i calculate sensitivity(True positive rate) and specificity(True negative rate) using libsvm as a binary classifier in matlab?

  15. 15

    Regex Lookahead & Lookbehind with Java

  16. 16

    How can I perform an OR with ScalaTest

  17. 17

    How to make negative rules in Apache?

  18. 18

    Negative Lookbehind Regex가 PHP에서 작동하지 않습니다.

  19. 19

    Negative Lookahead Regex가 내 문장을 무시합니다.

  20. 20

    Python3 re lookahead negative가 작동하지 않음

  21. 21

    for-loop to foreach with negative (i--) 문

  22. 22

    Karate - how to wait for assertion (negative assertion)

  23. 23

    How do you multiply negative numbers in javascript?

  24. 24

    How to convert a positive number to negative in batch?

  25. 25

    Regex Negative Lookbehind, 다른 단어가 앞에 나오지 않는 한 단어 일치

  26. 26

    Lookahead 및 Lookbehind 정규식

  27. 27

    Regex-Lookahead / LookBehind 어설 션

  28. 28

    How can I join a nested Perl hash?

  29. 29

    How i can handle http requests in perl?

뜨겁다태그

보관