简体   繁体   中英

perl sed escaping quantifiers from a regex

Having trouble removing quantifiers and meta characters from a regex with sed. To complicate matters the code is intended to be used within a eval.

$word_check = "about\s*[\w\s]+";
$word_check =~ s/\\\[.+\\\]//g;
$$word_count[$#$word_count + 1] += () = $word_check =~ /(\w+)\s*/g;

Word_check is a copy of a regex. Want to reduce the regex to only the words it is to match. Using a regex to do so. Then count the words and save the result in a array. The reason behind the choice is the regex is apart of several and when working with a match ($&) the words are removed. From a eval. The code below is intended as a string.

for ( my \$i = 0; \$i < \$\$ref_word_count[\$R]; \$i++ ) {
    \$temp_str =~ s/^\\w+\\s*//;
}

OK Found the answer. Just needed to break up the sed statement some and see the square brackets as a character alone.

$word_check =~ s/i$//;
$word_check =~ s/[\.\+]//g;
$word_check =~ s/\\[sdwSWD]//g;
$word_check =~ s/[[]//g;
$word_check =~ s/[]]//g;
$word_check =~ s/[*]//g;
$word_check =~ s/[?]//g;

Probably this helps

\\Q quote (disable) pattern metacharacters till \\E

\\E end either case modification or quoted section, think vi

See perldoc perlre

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM