Регулярные выражения: найти подстроку в предложении - PullRequest
0 голосов
/ 26 ноября 2018

У меня много предложений:

 1) the 3d line chart will show area in 3d.
 2) udcv123hi2ry32 the this line chart is useful.
 3) this chart.
 4) a chart.
 5) a line chart.
 6) this bar chart
 7) ...

И у меня есть условия

 1) substrings start by 'a' or 'the' or 'this' or '[chart name]'
 2) '[chart name] chart' is ok but 'this chart', 'a chart' are not accepted.
    (e.g. bar chart, line chart, this line chart, a area chart: OK,
     this chart, a chart: not accepted)
 3) substrings end by '.(dot)'

, следовательно, мне нужно найти подстроки, которые удовлетворяют условию.

В этом случаестрока:

"this line chart is very useful.", 
"area chart is very useful." are exactly what I want to receive.

Я пытаюсь это с помощью регулярного выражения, как это (https://regex101.com/r/aX5htr/2):

(a|the|this)* *((?!\bthis chart\b|\bwhich chart\b|\ba chart\b|\bthe chart\b|\bthat chart\b|\d+).+ chart) .+\.

, но не совпадает ...

как решить эту проблемуситуации ??

1 Ответ

0 голосов
/ 26 ноября 2018

Вы можете использовать

my $rx = qr/(?x)                 # enable formatting whitespace/comments
    (?(DEFINE)                   # Start DEFINE block
      (?<start>a|the|this|which) # Match start delimiters
    )                            # End DEFINE block
    (?<res>                      # Group res holding the match
      \b(?&start)\s+chart\b      # Match start delims, 1+ whitespace, chart
      (*SKIP)(*F)                # and skip the match
      |                          # or
      \b(?:(?&start)\s+)?        # Optional start delim and 1+ whitespace
      \w+\s+chart\b              # 1+ word chars, 1+ whitespace, char, word boundary
      [^.]*                      # 0+ chars other than dot
    )                            # End of res group
/;

См. Демо regex .

См. Демонстрацию Perl онлайн:

use strict;
use warnings;

my $rx = qr/(?x)                 # enable formatting whitespace/comments
    (?(DEFINE)                   # Start DEFINE block
      (?<start>a|the|this|which) # Match start delimiters
    )                            # End DEFINE block
    (?<res>                      # Group res holding the match
      \b(?&start)\s+chart\b      # Match start delims, 1+ whitespace, chart
      (*SKIP)(*F)                # and skip the match
      |                          # or
      \b(?:(?&start)\s+)?        # Optional start delim and 1+ whitespace
      \w+\s+chart\b              # 1+ word chars, 1+ whitespace, char, word boundary
      [^.]*                      # 0+ chars other than dot
    )                            # End of res group
/;
while (<DATA>) {
    if (/$rx/) {
        print "$+{res}\n";
    }
}

__DATA__
this chart.
this line chart.
this bar chart.
21684564523 this chart.
556465465456 this a line chart.
a chart.
a line chart.
which chart.
all this chart.
a chart.
123123 this chart..
123123 which chart.
all this line chart.
a line chart.
the 3d line chart will show area in 3d.
line chart.
area chart.
the chart.
1221513513 line chart.
1234125135 the chart.
123123 this bar chart.
udcvhi2ry32 the this line chart is useful.
twl chart.

Вывод:

this line chart
this bar chart
a line chart
a line chart
this line chart
a line chart
line chart will show area in 3d
line chart
area chart
line chart
this bar chart
this line chart is useful
twl chart
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...