Hello,
Consider this code:
use strict; use warnings; my $BEL_LETTERS = qr/ABVHD/; my @all_words = qw(tak het hen toj tah tam tym); ... while (my $next_line = <$FH>) { foreach my $next_word (@all_words) { my $count_words = ($next_line =~ s/\b($next_word[$BEL_LETTERS]*)\b/>$1</gi); } }
The substitution looks for words that begin with any of @all_words and end with zero or more of $BEL_LETTERS.
This code doesn't compile. Problem: the compiler thinks that $next_word[$BEL_LETTERS] is member $BEL_LETTERS of array @next_word.
This works - notice the parentheses:
s/\b($next_word([$BEL_LETTERS])*)\b/>$1</gi);
...But it feels like a hack to me. Is there a better way to overcome this interpolation?
I tried searching for the answer here in Q&A and in the Camel book, and strangely couldn't find it. I'm quite sure that the answer must be in the book, and if someone can point it to the page i'll be very thankful too.
(If anyone is curious, this code is looking for pronouns in a Belarusian text. This example here is very simplified - i didn't want to put the whole Belarusian-Cyrillic alphabet here).
In reply to variables in regex character classes by amir_e_a
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |