Word capture between regex extension

I have the following string type,

abc - xyz
abc - pqr - xyz
abc - - xyz
abc - pqr uvw - xyz

I want to get text xyzfrom the 1st line and pqrfrom the second line, `` (empty) from the 3rd and pqr uvw. The second hyphen is optional. abc- a static string, it should be there. I tried the following regular expression,

/^(?:abc) - (.*)[^ -]?/

But he gives me the following conclusion,

xyz
pqr - xyz
- xyz
pqr uvw - xyz

I do not need the last part in the second line. I use perl to create scripts. Can this be done using regex?

+4
source share
4 answers

, (.*) - , 0+, , , [^ -]?, - ? (1 0 ), . , pqr - xyz abc - pqr - xyz .

. .

/^abc\h*-\h*((?:[^\s-]+(?:\h+[^\s-]+)*)?)/

regex.

  • ^ -
  • abc - a abc
  • \h*-\h* - , 0+
  • ((?:[^\s-]+(?:\h+[^\s-]+)*)?) - 1,
    • [^\s-]+ - 1 , , -
    • (?:\h+[^\s-]+)* -
      • \h+ - 1 +
      • [^\s-]+ - 1 , , -
+3

^[^-]*-\s*\K[^\s-]*.

:

^       # Matches at the beginning of the line (in multiline mode)
[^-]*   # Matches every non - characters
-       # Followed by -
\s*     # Matches every spacing characters
\K      # Reset match at current position
[^\s-]* # Matches every non-spacing or - characters

.


: ^[^-]*-\s*\K[^\s-]*(?:\s*[^\s-]+)*

(?:\s*[^\s-]+)* , ().

+1

split:

$answer = (split / \- /, $t)[1];

$t - , , (.. [1], 0). , abc - - xyz, "-", 2 , . abc - - xyz , , :

$t =~ s/\- \-/-  -/;

, "-" , .

+1

?

, : - ^\s+ \s+$.

use strict;
use warnings; 
use 5.020;
use autodie;
use Data::Dumper;

open my $INFILE, '<', 'data.txt';

my @results = map {
    (undef, my $target) = split /-/, $_, 3;
    $target =~ s/^\s+//;  #remove leading spaces
    $target =~ s/\s+$//;  #remove trailing spaces
    $target;
} <$INFILE>;

close $INFILE;

say Dumper \@results;

--output:--
$VAR1 = [
          'xyz',
          'pqr',
          '',
          'pqr uvw'
        ];
0

Source: https://habr.com/ru/post/1693581/


All Articles